为什么添加“*”会MySQL 查询会导致语法错误吗?
在 MySQL 中,这段代码运行良好:
select f, blegg.* from blegg limit 1;
+------+------+------+------+
| f | f | g | h |
+------+------+------+------+
| 17 | 17 | 2 | 17 |
+------+------+------+------+
1 row in set (0.00 sec)
那么为什么这段代码会导致语法错误呢?
select f, * from blegg limit 1;
-- * is unqualified
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 '*
from blegg limit 1' at line 1
我翻阅了手册,但没有真正找到任何东西。为什么 select
失败,而 select
select * 。 ..
和 选择 *,
成功了吗?
In MySQL, this code works fine:
select f, blegg.* from blegg limit 1;
+------+------+------+------+
| f | f | g | h |
+------+------+------+------+
| 17 | 17 | 2 | 17 |
+------+------+------+------+
1 row in set (0.00 sec)
So why does this code cause a syntax error?
select f, * from blegg limit 1;
-- * is unqualified
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 '*
from blegg limit 1' at line 1
I've looked through the manual but didn't really find anything. Why does select <field>, * ...
fail where select <field>, <table>.* ...
and select * ...
and select *, <field> ...
succeed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是因为您选择同一字段两次。在以下查询中,
*
将包含name
,因此您将再次显式指定name
。这不是一个令人信服的论据,因为以下内容是有效的:
以下内容也是有效的,
两者都会多次选择同一字段。
更有可能的是,这只是 MySQL 的语法限制。
Possibly because you're selecting the same field twice. In the following query
the
*
will includename
, so you're explicitly specifyingname
a second time.This isn't a convincing argument because the following is valid:
and so is the following
both of which will select the same field multiple times.
More likely it is simply a syntax limitation of MySQL.
MySQL 手册在
SELECT 部分中非常清楚地列出了所有这些内容语法:
文档似乎表明
*
本身仅在特殊情况下有效,即它是选择列表中唯一的内容。但是,它只是说将不合格的*
与其他项一起使用可能会产生解析错误。除了 MySQL 之外,SQL-92 标准(旧的,但是可链接)说的是:
The MySQL manual lays all this out pretty clearly in the section on
SELECT
syntax:The documentation seems to indicate that
*
by itself is only valid in the special case where it's the only thing in the select list. However, it only says using an unqualified*
with other items may produce a parse error.Beyond MySQL, the SQL-92 standard (old, but linkable) says as much:
<select list>
can either be<asterisk>
by itself or a "normal" select list.但
会工作得很好。
可能不合格的 * 必须作为选择中的第一个表达式出现?
but
will work fine.
Possibly an unqualified * has to appear as the first expression in the select?