开发者

How can I return a list of records and true or false if a joined record exists using SQL?

开发者 https://www.devze.com 2023-04-08 23:14 出处:网络
I have three tables. Table A: TableA_ID, Description Table B: TableB_ID, TableA_ID, TableC_ID Table C: TableC_ID, Various Other columns

I have three tables.

Table A:

TableA_ID, Description

Table B:

TableB_ID, TableA_ID, TableC_ID

Table C:

TableC_ID, Various Other columns

Table B may contain zero or more recor开发者_运维技巧ds linking a record from Table C to a record in table A

I want a query that will return ALL records from Table A and an additional column that will be True or False dependent on whether any related records exist in Table B for a specific TableC_ID value.

Any help mucho appreciated.

Cheers

Stewart


SELECT a.TableA_ID, a.Description,
       CASE WHEN b.tableC_ID IS NOT NULL THEN 'True' ELSE 'False' END AS DoesExist
    FROM TableA a
        LEFT JOIN TableB b
            ON a.TableA_ID = b.TableA_ID
                AND b.TableC_ID = 123 -- Add your specific value here
0

精彩评论

暂无评论...
验证码 换一张
取 消