When editing some queries to add alternatives for columns without values, I accidentally wrote something like this (here is a simple version):
SELECT id, (SELECT name) FROM t
To my surprise, MySQL did not return any errors, but completed a query giving my expected results (values โโfor the name column). I tried to find documentation about this, but without success.
Is this SQL standard or MySQL specialty?
Can I be sure that the result of this syntax is indeed a column value from the same (external) table? An extended version will look like this:
SELECT id, (SELECT name FROM t AS t1 where t1.id=t2.id) FROM t AS t2
but EXPLAIN reports No tables used in the Extra column for the first version, which I think is very nice.
Here's a simple fiddle on SqlFiddle (it keeps time for me, hope you are more lucky).
Clarification: I know about subqueries, but I always wrote subqueries (correlated or not), which implied a table for selection, therefore, causing an additional step in terms of execution; my question is about this syntax and the result it gives that in MySQL it seems to return the expected value without any.
watery
source share