select a b from (select 1 a) b, (select 1 b union select 2) a; a) +---+ | a | +---+ | 1 | | 1 | +---+ b) +---+ | b | +---+ | 1 | | 1 | +---+ c) +---+ | b | +---+ | 2 | | 2 | +---+ d) +---+ | b | +---+ | 1 | +---+
[Just for fun]
mysql 5.0/5.1 questions for learning purposes
select a b from (select 1 a) b, (select 1 b union select 2) a; a) +---+ | a | +---+ | 1 | | 1 | +---+ b) +---+ | b | +---+ | 1 | | 1 | +---+ c) +---+ | b | +---+ | 2 | | 2 | +---+ d) +---+ | b | +---+ | 1 | +---+
[Just for fun]
You must be logged in to post a comment.
Answer b is correct. But why?
This query has two subqueries in the FROM-clause: a and b.
Subquery b results in
And a results in
We select column a from subquery b and give it the alias b.
This explains the ‘b’ as the column name in the result.
The subqueries are joined together without any restriction:
There is no on-clause or where-clause. That means, the result has 1 * 2 rows (cartesian product join).
This query is syntactically a bit confusing, but I hope it helps understanding subqueries, aliases and joins.