R left join and flag the record in another table
To perform a left join and flag the record in another table, you can use the following SQL query:
SELECT t1.*,
CASE WHEN t2.id IS NOT NULL THEN 'flagged' ELSE NULL END AS flag
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id;
In this query, we are selecting all columns from table1 and adding a new column called flag. We are using a CASE statement to check if the id exists in table2. If it does, we set the flag to 'flagged', otherwise, we set it to NULL.
The LEFT JOIN keyword is used to join table1 with table2 based on the id column. This will include all records from table1 even if there is no matching record in table2.
Overall, this query will return all records from table1 along with a flag indicating whether or not there is a matching record in table2
原文地址: https://www.cveoy.top/t/topic/fJ9E 著作权归作者所有。请勿转载和采集!