【问题标题】:mySQL query to Retrieve only the records in left tablemySQL查询仅检索左表中的记录
【发布时间】:2017-05-30 11:31:06
【问题描述】:

如何编写 mysql 查询以获取左表中的记录。
Left Join 为我提供了 T1 表中的所有记录,而 mySQL 中没有 MINUS

编辑:
不希望使用子查询

【问题讨论】:

标签: mysql database join


【解决方案1】:

您正在寻找一个左排除连接

  SELECT  A.*
  FROM Table_A A
  LEFT JOIN Table_B B
  ON A.Key = B.Key
  WHERE B.Key IS NULL

请参阅这篇关于 SQL 连接的文章,它会有所帮助
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

【讨论】:

    【解决方案2】:

    使用not exists:

    select l.*
    from `left` l
    where not exists (select 1 from `right` r where r.id = l.id);
    

    如果需要更多列比较,可以展开逻辑:

    select l.*
    from `left` l
    where not exists (select 1
                      from `right` r
                      where r.col1 = l.col1 and
                            r.col2 = l.col2 and
                            . . .
                     );
    

    【讨论】:

    • 实际上是在寻找没有任何子查询的查询。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多