请问下面这个 sql 语句哪里有错? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kanilu
V2EX    问与答

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

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

    运行下面的 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     Solana     3250 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 11:41 PVG 19:41 LAX 04:41 JFK 07:41
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86