MySQL return set if values don't exist

118 Views Asked by At

Let's say I have 5 rows, containing the following:

ID     KEY       VALUE
1      address   123 Main Street
1      latlng    (111.139, 111.238)
2      address   45 South Street
3      address   67 North Ave
3      latlng    (448.002, 100.301110)

Now, if I wanted to return ONLY the row that did NOT contain a corresponding latlng row to accompany the address (ie, ID 2) how would I go about doing that with a mySQL statement?

This one has me stumped!!

2

There are 2 best solutions below

1
On BEST ANSWER
SELECT *
FROM TABLE
WHERE ID NOT IN
(SELECT ID FROM TABLE WHERE KEY = 'LATLNG')
1
On

Try something like

SELECT t1.*
FROM Table t1
    LEFT OUTER JOIN Table t2 ON t1.id = t2.id
WHERE t1.Key = 'latlng' AND t2.Key = 'address' AND t2.id IS NULL