Home > Net >  How do I query all records from table A and similar matches from table B as a boolean in a different
How do I query all records from table A and similar matches from table B as a boolean in a different

Time:12-15

I am trying to query from tables A and B that returns all the records from table A and indicates matches (ID's that exist in both tables) as true in a field called "approved"

TABLE A

ID
2
4
6
8
10

TABLE B

ID
2
6
8

WHAT I TRIED

This returns the results times the number of records in table B, but is of course not what I am looking for.

SELECT [Source A].ID, IIf([Source A].[ID]=[Source B].[ID],True,False) AS approved
FROM [Source A], [Source B];

DESIRED RESULT

ID MATCH
2 TRUE
4
6 TRUE
8 TRUE
10

CodePudding user response:

You can try in this way

SELECT table_a.ID, IIf(table_a.[ID]=table_b.[ID],'True','False') AS approved
FROM table_a
LEFT JOIN table_b ON table_a.id = table_b.id

It will then show true and false accordingly

CodePudding user response:

SELECT A.ID,
CASE
  WHEN B.ID IS NULL THEN 'NO MATCH'
  ELSE 'MATCH'
END FLAG
FROM TABLE_A AS A
LEFT JOIN TABLE_B AS B ON A.ID=B.ID

May be something like this

  • Related