なぜこれが構文エラー(MySQL 5)を引き起こすのですか?
mysql> select f, blegg.* from blegg limit 1; +------+------+------+------+ | f | f | g | h | +------+------+------+------+ | 17 | 17 | 2 | 17 | +------+------+------+------+ 1 row in set (0.00 sec) mysql> 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 , * ... fail where select , <table>.* ... and select * ... and select *, ... succeed?
select , * ...
select , <table>.* ...
select * ...
select *, ...
MySQLのマニュアルでは、 SELECT 構文:
SELECT
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables: SELECT * FROM t1 INNER JOIN t2 ... tbl_name.* can be used as a qualified shorthand to select all columns from the named table: SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ... Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference SELECT AVG(score), t1.* FROM t1 ...
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables:
*
SELECT * FROM t1 INNER JOIN t2 ...
tbl_name.* can be used as a qualified shorthand to select all columns from the named table:
tbl_name.*
SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ...
Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference
SELECT AVG(score), t1.* FROM t1 ...
ドキュメントは、 * だけが選択リストの唯一の特殊な場合にのみ有効であることを示しているようです。ただし、解析エラーを生成する可能性があります以外の項目では、修飾されていない *
MySQL以外にも、 SQL-92標準(古いものですが、リンク可能な)
7.9 Format ::= SELECT [ ] <table expression> ::= | [ { }... ] ::= | ::= [ ] ::= [ AS ]
can either be by itself or a "normal" select list.
select *、count(*)...
同じフィールドを2回選択している可能性があります。次のクエリ
select name, * from <...>
* には name が含まれるので、 name を明示的に指定しています。
name
以下は有効なので、これは説得力のある議論ではありません:
select name, name from <...>
以下も同様です
select name, users.* from users
どちらも同じフィールドを複数回選択します。
おそらくMySQLの構文上の制限にすぎないでしょう。
select count(*)、* blegg
f
しかし
select *, f from blegg
うまく動作します。
おそらく、非修飾*が選択の最初の式として表示される必要がありますか?