user6854154 user6854154 - 2 months ago 15
SQL Question

SQL - Select query

Here is my simulated data

Table 1 Table 2
Column 1 Column 2 Column 1 Column 2
111 AAA 111 AAA
111 BBB 111 BBB
222 AAA 111 CCC
... 222 AAA
...


I want the 'children' (Column2) of Table 1 in Table 2 which are absent in Table 1. In this case I would need
'111 CCC'
.

Answer

I would use CONCAT. The way I read your question, you are not just checking that CCC is in table 1, but rather, you want to know if the combination of 111 AND CCC is in table 1. You can do that as follows:

SELECT column1, column2
FROM table2
WHERE CONCAT(column1, column2)
NOT IN
(SELECT CONCAT(column1, column2)
 FROM table1)
Comments