V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kanilu
V2EX  ›  问与答

请问下面这个 sql 语句哪里有错?

  •  
  •   kanilu · 2017-07-09 22:47:50 +08:00 · 2401 次点击
    这是一个创建于 2454 天前的主题,其中的信息可能已经有所发展或是发生改变。

    运行下面的 sql 语句 总是说 query 出错

        use employees;
        set autocommit=0;
        begin;
        SELECT * from employees where emp_no=10002;
        commit;
    
    15 条回复    2017-07-10 09:49:51 +08:00
    kanilu
        1
    kanilu  
    OP
       2017-07-09 22:51:54 +08:00
    喔,对了,上面的 employees 是一个数据库,里面有一个表也叫 employees
    kanilu
        2
    kanilu  
    OP
       2017-07-09 23:06:25 +08:00
    顶顶顶
    zhusimaji
        3
    zhusimaji  
       2017-07-09 23:11:14 +08:00 via iPhone
    你要贴具体的错误信息....
    kanilu
        4
    kanilu  
    OP
       2017-07-09 23:15:03 +08:00
    @zhusimaji

    SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * from employees where emp_no=10002;
    commit' at line 2
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * from employees where emp_no=10002;
    commit' at line 2
    wdlth
        5
    wdlth  
       2017-07-09 23:23:16 +08:00
    开始事务是 begin transaction 吧,你输入的是 begin,就变成了 begin...end 流程控制了。
    kanilu
        6
    kanilu  
    OP
       2017-07-10 00:00:24 +08:00
    @wdlth 改成 begin transaction
    还是错误啊
    misaka19000
        7
    misaka19000  
       2017-07-10 00:19:27 +08:00 via Android
    查询还有事务吗,第一次看到
    11138
        8
    11138  
       2017-07-10 00:28:33 +08:00
    语法没有问题,问题在于你的用法有问题,你应该帖出相关的使用代码。
    kiwi95
        9
    kiwi95  
       2017-07-10 00:36:05 +08:00 via Android
    不知道你是要用 begin-end 控制块还是要用事物,按说你就一个 select 不需要用事物。
    如果用 begin-end 你的写法不对,如果用事物应该用 start transaction,不是 begin transaction。
    给你贴个文档 https://dev.mysql.com/doc/refman/5.7/en/begin-end.html
    kiwi95
        10
    kiwi95  
       2017-07-10 00:38:13 +08:00 via Android
    既然是提问就多提供些信息,什么数据库,什么版本,具体报什么错,没人会去给你跑一下你的语句给你分析
    11138
        11
    11138  
       2017-07-10 00:39:52 +08:00
    mysql> use employees;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)

    mysql> begin;
    Query OK, 0 rows affected (0.00 sec)

    mysql> SELECT * from employees where emp_no=10002;
    +----+--------+
    | id | emp_no |
    +----+--------+
    | 1 | 10002 |
    +----+--------+
    1 row in set (0.00 sec)

    mysql> commit;
    Query OK, 0 rows affected (0.00 sec)

    mysql> use employees;set autocommit=0;begin;SELECT * from employees where emp_no=10002;commit;
    Database changed
    Query OK, 0 rows affected (0.00 sec)

    Query OK, 0 rows affected (0.00 sec)

    +----+--------+
    | id | emp_no |
    +----+--------+
    | 1 | 10002 |
    +----+--------+
    1 row in set (0.00 sec)

    Query OK, 0 rows affected (0.00 sec)
    11138
        12
    11138  
       2017-07-10 00:45:33 +08:00
    https://dev.mysql.com/doc/refman/5.7/en/commit.html
    START TRANSACTION or BEGIN start a new transaction.
    tomatoz
        13
    tomatoz  
       2017-07-10 04:37:34 +08:00
    目测你把 commit 和其他语句写一起了。。
    应该这样吧:
    ……
    statement.execute("blablabla");
    conn.commit();
    ……
    另外查询操作也用不到事务 +1
    heaton_nobu
        14
    heaton_nobu  
       2017-07-10 09:21:30 +08:00
    这是在 commit 什么
    leejanfin
        15
    leejanfin  
       2017-07-10 09:49:51 +08:00
    单纯看这个 SQL 没发现什么问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   944 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:57 · PVG 04:57 · LAX 13:57 · JFK 16:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.