I'm getting a syntax error on the following query:
SELECT 1,2 WHERE 1=1
But this query works fine:
SELECT 1,2 FROM (SELECT 1) t WHERE 1=1;
It almost looks like a WHERE clause always needs a table. Sometimes, in the depth of a complex query it's nice to use a SELECT/WHERE combo to turn on and off certain features. Is 开发者_如何学Gothere a way to not always add the FROM (SELECT 1) t
?
Edit:
I found another similar issue
(SELECT 1 x)
UNION
(SELECT 2)
WHERE 1=1
gives a syntax error, but this does not:
SELECT x
FROM
(
(SELECT 1 x)
UNION
(SELECT 2)
) t
WHERE 1=1
I'm using 5.1.48-community MySQL Community Server (GPL)
. Is anyone else seeing this?
You can use "FROM DUAL" to just say you're not selecting from a table. As in :
SELECT 'Hello, World' FROM DUAL;
This:
SELECT 1 x
UNION
SELECT 2
WHERE 1 = 1
is two queries, combined with UNION
. The second query:
SELECT 2
WHERE 1 = 1
is invalid, since it misses a FROM
clause while still using WHERE
.
This:
SELECT x
FROM (
SELECT 1 x
UNION
SELECT 2
) t
WHERE 1 = 1
is a SELECT
from an inline view, which is again, two queries combined with a UNION
.
Neither of the queries uses WHERE
, so it's OK for them not to have a FROM
. Both queries are valid, and the resulting query is too.
精彩评论