【问题标题】:show the names of all suppliers that appear more than once in the supplies table显示在供应表中出现多次的所有供应商的名称
【发布时间】:2022-01-12 11:40:44
【问题描述】:

我有 2 个表耗材和供应商,我需要打印在耗材表​​中出现多次的所有供应商的名称,我尝试使用以下代码:

SELECT name FROM SUPPLIER
WHERE supplier.supplierNum = supplies.supplerNum
having count(supplierNum) > 1;

但是代码 i=没有工作。 这是表格:

CREATE TABLE supplies (
  supplierNum CHAR(2) NOT NULL,
  partNum CHAR(2) NOT NULL,
  quantity SMALLINT(6) NOT NULL,
  PRIMARY KEY (supplierNum, partNum),
  FOREIGN KEY (supplierNum) REFERENCES supplier (supplierNum),
  FOREIGN KEY (partNum) REFERENCES parts (partNum)
);

CREATE TABLE supplier (
  supplierNum CHAR(2)   NOT NULL,
  name CHAR(10) NOT NULL,
  status TINYINT(4) NOT NULL,
  city VARCHAR(10)  NOT NULL,
   PRIMARY KEY (supplierNum)
);

【问题讨论】:

标签: mysql sql jdbc


【解决方案1】:

首先计算supplies 表中的出现次数,然后使用supplier 计算join 中的出现次数:

SELECT sp.name
FROM supplier AS sp
INNER JOIN 
  (
  SELECT distinct supplierNum 
  FROM supplies
  GROUP BY supplierNum
  HAVING COUNT(supplierNum) > 1
  ) as cnt
 ON sp.supplierNum = cnt.supplierNum;

dbfiddle 中的演示 here

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2015-04-07
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多