I have two tables
tbl1:
id
userid
subscriptionid
column2
column3
tbl2:
id
userid
subscriptionid
column4
column5
Column6
Column7
The subscriptionid values are unique, and would only exist in tbl1 or tbl2, but may also be null
I need a query to find a subscriptionid in either tbl1 or tbl2
SELECT * FROM tbl1, tbl2 WHERE (tbl1.subscriptionid OR tbl2.subscriptionid) = 'some_val';
2
Answers
You can achieve this by using the SQL UNION operator to combine the results from both tables and then filter for the specific value you’re looking for. Here’s how you can write the query:
This query will return a result set that includes the subscriptionid values from either tbl1 or tbl2 where the subscriptionid matches the specified value ‘some_val’. The UNION operator ensures that duplicate values are removed from the result set, and you’ll get a unique list of matching subscriptionid values from both tables.
I hope this solves your problem..
Just normalize the
SELECT
list between the two queries in theUNION ALL
:Here’s a db<>fiddle.