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 there 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?
This:
is two queries, combined with
UNION. The second query:is invalid, since it misses a
FROMclause while still usingWHERE.This:
is a
SELECTfrom an inline view, which is again, two queries combined with aUNION.Neither of the queries uses
WHERE, so it’s OK for them not to have aFROM. Both queries are valid, and the resulting query is too.