Get values of one MySQL table that are not in other table

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts: 107

Get values of one MySQL table that are not in other table

Hello,
I have two tables: A and B. I need to compare their columns to get values of one table that are not in other table.
  • Table A columns : ID, table_id (key reference to table B), data
    Table B columns : ID (primary key, references to table A), x, y
I need to get values from A.table_id that are not equal to B.ID

Code: Select all

A.table_id values = 3,3,2
B.ID values = 1,2,3
I need to get value 1.

Admin Posts: 805
Hi,
You can use not in:

Code: Select all

SELECT *
FROM table_A 
WHERE table_id NOT IN (SELECT id from table_B)