MySQL select rows that do not have matching column in other table

前端 未结 3 1577
南笙
南笙 2020-12-13 13:21

I can\'t seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets

3条回答
  •  不思量自难忘°
    2020-12-13 13:32

    Typically, you would use NOT EXISTS for this type of query

    SELECT p.Name
    FROM   pooltest p
    WHERE  NOT EXISTS (SELECT s.Name
                       FROM   senttest s
                       WHERE  s.Name = p.Name)
    

    An alternative would be to use a LEFT OUTER JOIN and check for NULL

    SELECT p.Name
    FROM   pooltest p
           LEFT OUTER JOIN senttest s ON s.Name = p.Name
    WHERE  s.Name IS NULL
    

    Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.

提交回复
热议问题