Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so i wanna join two of my table together and the result must be like if table A exist then choose table A else Table B

What I have tried:

Select 
      id, 
      name,
      age

FROM TableA as a
WHERE NOT EXISTS ( SELECT * FROM TableB WHERE a.id = b.id)

UNION ALL

Select 
      id, 
      name,
      age

FROM TableB as b;


is it the correct way to use NOT EXISITS and UNION ALL?
Posted
Updated 31-Aug-20 23:31pm
v2

1 solution

Quote:
if table A exist then choose table A else Table B
The code you've shown would do the opposite - it will only return the record from table A if there isn't a matching record in table B.

Try something like this:
SQL
SELECT
    id,
    name,
    age
FROM
    TableA

UNION ALL

SELECT
    id,
    name,
    age
FROM
    TableB As b
WHERE
    NOT EXISTS
    (
        SELECT *
        FROM TableA As a
        WHERE a.id = b.id
    )
;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900