How to get from a sub where id does not exist

I have a sql query that retrieves results from a table, but I need it to only show results if the identifiers are not in another table.

Example

$sql = "SELECT id FROM TABLE_1 WHERE id NOT IN(SELECT more_id FROM TABLE_2)

The idea is that if the identifier does not exist in the "more_id" list, it should show the result.

It does not seem to work, any help would be greatly appreciated. It should be noted that "more_id" is the same "id", but in a different table in which other records are stored.

+3
source share
2 answers

This should work:

$sql = "SELECT TABLE_1.id
            FROM TABLE_1
            LEFT JOIN TABLE_2
                ON TABLE_1.id = TABLE_2.more_id
            WHERE TABLE_2.more_id IS NULL"

WHERE TABLE_1, , TABLE_2, , . WHERE .

+4

VARCHAR id, INTEGER, CHAR .

0

Source: https://habr.com/ru/post/1723733/


All Articles