MySQL Join Where Not Exists

前端 未结 3 1257
孤街浪徒
孤街浪徒 2020-12-02 10:12

I have a MySQL query that joins two tables

  • Voters
  • Households

They join on voters.household_id and household.id

3条回答
  •  醉梦人生
    2020-12-02 10:57

    I'd probably use a LEFT JOIN, which will return rows even if there's no match, and then you can select only the rows with no match by checking for NULLs.

    So, something like:

    SELECT V.*
    FROM voter V LEFT JOIN elimination E ON V.id = E.voter_id
    WHERE E.voter_id IS NULL
    

    Whether that's more or less efficient than using a subquery depends on optimization, indexes, whether its possible to have more than one elimination per voter, etc.

提交回复
热议问题