When using a subquery in a WHERE conditional the query returns empty set:
If I do: (1)
USE general_db;
SELECT * FROM general_db.master WHERE alert IN ('M1_001','G1_011');
+------+-------------+
| mkey | alert |
+------+-------------+
| 17 | G1_011 |
| 19 | M1_001 |
+------+--------+----+
(1) works as expected.
Then I do: (2)
SELECT DISTINCT TABLE_NAME AS 'alert' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='maintenance_db';
+-------------+
| alert |
+-------------+
| G1_011 |
| M1_001 |
+-------------+
(2) Works as expected.
But then if I use the second query (2) as a subquery of the first (1): (3)
SELECT * FROM general_db.master WHERE alert IN (SELECT DISTINCT TABLE_NAME AS 'alert' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='maintenance_db');
Empty set (0,00 sec)
(3) does not work as expected. I would think it should return the same results as in (1).
How can I do for the combination of both (3) to return the same results as in (1)? And why is this query (3) not working?
More info:
As you have may noticed, the table names in maintenance_db, may be contained in a column named alert in master table thats inside the general_db.
It is very strange. I am sure the subquery syntax is correct, this query works fine:
SELECT * FROM general_db.master WHERE alert IN (SELECT alert FROM general_db.master WHERE alert IN ('G1_011','M1_001'));
So I think it may be something related to query (2).
This works:
USE maintenance_db;
select * from M1_001;
select * from G1_011;
So the names are correct I guess.
Thanks