MySQL / SQL 的“Using”简写形式可以吗?不用说“内连接”就可以使用吗?
下面2条语句是用usinggiftID=sentgifts.giftID:来连接的,
mysql> select * from gifts, sentgifts using (giftID);
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 'using
(giftID)' at line 1
第二条语句:
mysql> select * from gifts INNER JOIN sentgifts using (giftID);
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
| giftID | name | filename | effectiveTime | sentID | whenSent | fromID | toID | trytryWhen |
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
| 2 | teddy bear | bear.jpg | 2010-04-24 04:36:03 | 4 | 2010-04-24 | NULL | 111 | 2010-04-24 03:10:42 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 5 | 2010-03-03 | 11 | 22 | 2010-03-03 00:00:00 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 6 | 2010-04-24 | 11 | 222 | 2010-04-24 03:54:49 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 7 | 2010-04-24 | 1 | 2 | 2010-04-24 03:58:45 |
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
4 rows in set (0.00 sec)
第一个语句也可以使用“using”简写吗?好像使用的时候必须指定“Inner Join”这个词...但是第一条语句实际上是一个inner join?
更新:如果无法完成,是否有原因?第一个声明实际上已经足够清楚了要做什么......有什么理由禁止它吗?
The following 2 statements are to join using gifts.giftID = sentgifts.giftID:
mysql> select * from gifts, sentgifts using (giftID);
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 'using
(giftID)' at line 1
and the second one:
mysql> select * from gifts INNER JOIN sentgifts using (giftID);
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
| giftID | name | filename | effectiveTime | sentID | whenSent | fromID | toID | trytryWhen |
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
| 2 | teddy bear | bear.jpg | 2010-04-24 04:36:03 | 4 | 2010-04-24 | NULL | 111 | 2010-04-24 03:10:42 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 5 | 2010-03-03 | 11 | 22 | 2010-03-03 00:00:00 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 6 | 2010-04-24 | 11 | 222 | 2010-04-24 03:54:49 |
| 6 | beer | beer_glass.png | 2010-04-24 05:18:12 | 7 | 2010-04-24 | 1 | 2 | 2010-04-24 03:58:45 |
+--------+------------+----------------+---------------------+--------+------------+--------+------+---------------------+
4 rows in set (0.00 sec)
Can the first statement also use the "using" shorthand? It seems that when it is used then the word "Inner Join" must be specified... but the first statement is actually an inner join?
Update: if it can't be done, is there a reason why? The first statement actually is all clear enough what is meant to be done... is there a reason why it is prohibited?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不能。您必须在要连接的表之间使用
JOIN
关键字,才能使USING
正常工作。以下是相关语法,来自 MySQL 手册
: ,目前尚不清楚您要使用您使用的第一个语法来完成什么任务。它可以是外部连接或内部连接。您可以争辩说,当不存在时,解释器应该将其视为内部联接,但这会令人困惑。此外,这将是一种新的非标准语法,恕我直言,这绝对是不行的。
It can not. You have to use the
JOIN
keyword between the tables you want to join, in order forUSING
to work.Here is the relevant grammar, from the MySQL manual:
Also, it is not that clear what you are trying to accomplish with the first syntax you are using. It could be an outer or an inner join. You can argue that, when absent, the interpreter should consider it an inner join, but that would be confusing. Additionally, it would be a new, non-standard syntax, which is definitely a no no, imho.
使用条件只是为了防止您必须像这样输入它。
因此,您无需键入 ON 条件。您仍然必须指定它是内部联接。
根据您的编辑,它不起作用的原因是您没有指定您想要执行的联接类型。你在做内连接吗?左连接?外连接?服务器的 SQL 解释器无法假定您正在使用哪一个。
The using condition is only there to prevent you from having to type it out like this.
So it prevents you from having to type out that ON condition. You still have to specify that it is an inner join.
In response to your edit, the reason why it doesn't work is because you aren't specifying what kind of join you want to do. Are you doing an inner joing? Left join? Outer join? The server's SQL interpreter can't assume which one you are using.