首页 > MySQL语句优先级问题,如IN后面的子句似乎必须要带小括号,有时候多了小括号又出错?

MySQL语句优先级问题,如IN后面的子句似乎必须要带小括号,有时候多了小括号又出错?

mysqlmysql> select c from tb8 where c in select b from tb6;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corre
sponds to your MySQL server version for the right syntax to use near 'select b from t
b6' at line 1
mysql> select c from tb8 where c in (select b from tb6);
+------+
| c    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)
sqlmysql> insert into tb11 (select * from tb6);
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into tb11 (select * from tb6) union (select * from tb7);
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

--下面这句有疑问
mysql> insert into tb11 (select * from tb6 union select * from tb7);
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 'union select * from tb7)' at line 1

mysql> insert into tb11 select * from tb6 union select * from tb7;
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

如c++很容易通过{, }来分辨一段代码的层次,MySQL感觉有些杂乱,不知道有何诀窍。


可以加一些回车来区分,只要不遇到;就不算结束。所以上句可以改写为:

insert into tb11
select * from tb6
union select * from tb7;

【热门文章】
【热门文章】