I have a query against the sakila database in MySQL 8(You can easily setup one from https://hub.docker.com/r/1maa/sakila).
select * from store _0 where (
select address_id from (
select * from address _3 where (
address_id = (
select address_id from (
select * from store _1
where store_id=_0.store_id
) _2
)
)
) _4
);
This query returns an empty table.
However, when I rewrite the innermost
select address_id from (
select * from store _1
where store_id=_0.store_id
) _2
into:
select address_id from store _1
where store_id=_0.store_id
, it returns all 2 rows from store
table, which I expect.
Why is this? It seems I just simplified a 2-step select into 1.
I also noticed that if I use in
instead of =
in address_id = ..
condition, I can get the expected result.
What's the theory behind this?
The query is over-complicated because it's generated. So thank you for your advice of simplified queries but I just want to know why it does not give the correct result.
And you must have MySQL 8.0.14 and above to reproduce it, because earlier versions does not allow alias beyond 1 level.