【问题标题】:Comparing two tables and finding mismatches比较两个表并发现不匹配
【发布时间】:2011-02-14 17:42:36
【问题描述】:

我有两个 sql 表,我想将它们相互对照以找到不匹配的表。

我有一些有用的东西,但由于某种原因错过了两条记录?

表 flag_content 包含: - 用户身份 - content_id

table topfive_order 包含 - 尼德 - uid

我希望找到所有在 flag_content.content_id 中不存在 topfive_order.nid 的记录

我目前的查询是:

select * from flag_content left join topfive_order topfive_order ON flag_content.content_id = topfive_order.nid WHERE topfive_order.nid is null

非常欢迎任何提示或建议。我不太清楚我用左连接做了什么。所以我认为这几条漏网的记录与此有关。

【问题讨论】:

  • 查询似乎正确。你缺少的两条记录,它们在topfive_order 表中吗?

标签: sql mysql


【解决方案1】:

翻转连接

SELECT *
FROM topfive_order topfive_order left join flag_content 
  ON flag_content.content_id = topfive_order.nid 
WHERE flag_content.content_id IS NULL

要从topfive_order 表中查找flag_content 表中不存在的行,您需要将topfive_order 放在LEFT JOIN 的左侧。

有关各种联接类型的更多信息,请参阅Wikipedia

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多