我需要从表 1 中选择一些行假设是否在表 2 中找到了一个值。所以我想检查是否在表 2 中找到该值(我将从命令行输入值)然后选择行来自 Table1,如果不是,我想从另一个表中选择行。 我试过 CASE 但从我得到的结果来看,只有当你想检查一个表中的值时才有效。有什么想法吗?
最佳答案
你可以这样做:
-- If value is found in table2, select from table1
select * -- <- use padding if necessary
from table1
where exists (select 1
from table2
where myField = value)
union all
-- If value is not found in table2, select from another_Table
select * -- <- use padding if necessary
from another_Table
where not exists (select 1
from table2
where myField = value)
关于mysql - 如果在另一个表中找到记录,则从表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310838/