I have two tables in a MySQL database - t1
with a column c1
, and t2
with a column c2
.
I run this query:
select * from t1 where c1 in (select c1 from t2);
The above query should give an error as c1
is not present in t2. Instead, it returns all the rows from t1
. Another version of the above query with delete
which can be much more disastrous:
delete from t1 where c1 in (select c1 from t2);
The above query deletes all the rows from t1 when it is just supposed to give an error.
I have noticed this behavior occurs only when the column in the subquery has the same name as the outer one. Meaning,
select * from t1 where c1 in (select c3 from t2);
will throw an error as expected:
ERROR 1054 (42S22): Unknown column 'c3' in 'field list'
By the way, I have checked for the same issue on PostgreSQL 9.6.3 and the behavior is exactly the same. Any explanation for this strange behavior?