【问题标题】:mysql checking if product ID also exist in other table [duplicate]mysql检查产品ID是否也存在于其他表中[重复]
【发布时间】:2023-03-03 23:24:01
【问题描述】:

我对 JOINS 没有太多经验,并且通过下面的查询得到的结果不正确。 我有一个名为 products 的表,想检查表 product_links 中是否有记录。 我只想获取 product_links 中没有行的项目列表。

当我运行以下查询时,我只得到一行。 有人建议吗? Google 无法帮助我,或者我使用了错误的关键字进行搜索。

SELECT a.id, a.SKU, a.title, 
(SELECT COUNT(b.id) AS amount FROM product_links WHERE b.product=a.id) AS amount
FROM products AS a
LEFT JOIN product_links AS b ON b.product=a.id

【问题讨论】:

  • 提示:养成在浏览器中输入问题有意义部分的习惯。例如,输入mysql get a list of items that doesn't have rows in other table 将立即为您提供答案。

标签: mysql join count


【解决方案1】:

我会推荐not exists

select p.*
from products p
where not exists (select 1 from product_links pl where pl.product_id = p.id)

【讨论】:

    【解决方案2】:

    根据您的问题,我了解到您需要没有任何链接的产品信息。

    下面是那个查询

    select * from FROM products 
    where id not in (SELECT id FROM product_links);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 2022-08-03
      • 2017-10-28
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多