1

I have two tables as shown below they have a one to many relationship. match table (one) message_log table (many) using match_id as a FK

Table 1

enter image description here

Table 2

enter image description here

I'm trying to write an sql statement that would return all the match_ids that do not exist in the message_log table.

Arya
  • 8,473
  • 27
  • 105
  • 175
  • possible duplicate of [SQL - find records from one table which don't exist in another](http://stackoverflow.com/questions/367863/sql-find-records-from-one-table-which-dont-exist-in-another) – Palpatim Jun 09 '15 at 16:00
  • 2
    Please post table definitions and sample data as formatted ***text***, not as screenshots. –  Jun 09 '15 at 16:02

2 Answers2

2

This will give you all the match_ids, in the match table, that do not exist in the message_log table:

SELECT 
m.match_id
FROM match m
WHERE 
m.match_id NOT IN
(
    SELECT
    ml.match_id 
    FROM message_log ml
)

Example SQL Fiddle: http://sqlfiddle.com/#!15/bacb5/1/0

Donal
  • 31,121
  • 10
  • 63
  • 72
0
SELECT m.match_id 
  FROM "match" m LEFT OUTER JOIN "message_id" g ON m.match_id = g.match_id
 WHERE g.match_id IS NULL

Try different solutions, as the optimizer will sometimes do interesting things.