select count(*)
from
(select emp_id from table_1
union
select emp_id from table_2) t
where t.emp_id = <id_value>
How to check if a value exists in multiple tables
سؤال
If i have 2 tables say TABLE_1
EMP_ID EMP_NAME EMP_COUNTRY
100 John Russia
101 Mitchell UK
102 Sarah Japan
TABLE_2
EMP_ID EMP_NAME EMP_COUNTRY
200 Sunil India
201 Clanton Germany
202 XYZ Australia
I want to check whether EMP_ID exists in [table_1 OR table_2] if it exists in one of the tables then based on that set some flag, How to check this.
المحلول
نصائح أخرى
You can also try:
SELECT DECODE((
SELECT SUM(CNT) FROM
(SELECT COUNT(1) CNT FROM TABLE1 WHERE EMP_ID = yr_emp_id
UNION
SELECT COUNT(1) CNT FROM TABLE2 WHERE EMP_ID = yr_emp_id)),
0,'FALSE','TRUE')
FROM DUAL;
If (Select Count(*) From TABLE_1 Where EMP_ID = @EMP_ID) > 0
Begin
-- set flag
End
Else If (Select Count(*) From TABLE_2 Where EMP_ID = @EMP_ID) > 0
Begin
-- or set flag here
End
لا تنتمي إلى StackOverflow