如果在另一个表中找到记录,则从表中选择 [英] Select from table if record found in another table
本文介绍了如果在另一个表中找到记录,则从表中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从表1中选择一些行,要说是否在表2中找到了一个值.因此,我想检查是否在表2中找到了该值(我将从命令行输入该值),然后选择行从Table1,如果不是,我想从另一个表中选择行. 我尝试了CASE,但是只有当您想检查一个表中的值时,我才能从CASE中获得结果.有什么主意吗?
I need to select some rows from Table 1 lets say if a value is found in Table 2. So I want to check if the value (I will enter the value from command line) is found in Table 2 and then select rows from Table1, if not I want to select rows from another table. I tried CASE but from what I got that works only if you want to check for value within one table. Any idea?
推荐答案
您可以执行以下操作:
-- 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)
这篇关于如果在另一个表中找到记录,则从表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文