gpt4 book ai didi

MySql查询代码从单个ID中获取唯一数据

转载 作者:行者123 更新时间:2023-12-01 00:35:13 25 4
gpt4 key购买 nike

如何列出订购化学品 [NUMBER] 的任何客户的 CUSTNUM 和名称。

订单

+---------+--------+------------+------+
| CUSTNUM | CHEMNO | DATE | QTY |
+---------+--------+------------+------+
| 123456 | 1234 | 2000-00-00 | 35 |
+---------+--------+------------+------+

客户

+---------+-----------+-----------+
| CUSTNUM | NAME | LOCATION |
+---------+-----------+-----------+
| 123456 | AmChem | New York |
+---------+-----------+-----------+

最佳答案

您可以加入 CUSTOMERORDERS包含特定 <chemno> 订单的表格使用仅购买产品的 custnum 的子查询:

SELECT
CUSTNUM, NAME
FROM
CUSTOMER c
INNER JOIN
ORDERS o ON o.CUSTNUM = c.CUSTNUM and o.CHEMNO = <chemno>
INNER JOIN
( SELECT
CUSTNUM
FROM
ORDERS
GROUP BY
CUSTNUM
HAVING
COUNT(DISTINCT CHEMNO) = 1 ) t ON t.CUSTNUM = o.CUSTNUM

关于MySql查询代码从单个ID中获取唯一数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com