gpt4 book ai didi

mysql - 在 MySQL 表中查找 "exotic"值

转载 作者:可可西里 更新时间:2023-11-01 06:57:49 25 4
gpt4 key购买 nike

我有一个名为 accounts 的 MySQL 表。此表中有一个名为 salesmanager_id 的字段。 99% 的情况下,此字段中的值对于特定客户(用 customer_id 指定)的所有帐户始终相同。

我想做的是为帐户分配给多个销售经理的客户找到 customer_id。例如:

+------------------------------------+
| id | customer_id | salesmanager_id |
|------------------------------------|
| 1 | 12 | 4 |
| 2 | 12 | 4 |
| 3 | 14 | 3 | <-- I want this customer_id
| 4 | 14 | 4 |
+------------------------------------+

在上面的示例中,customer_id 14 同时分配了 salesmanager_id 3 和 4。我想为我的列表检索那个 customer_id

我尝试了以下查询,但返回了一个空结果(虽然我确信至少存在一些差异)。

SELECT `name`, `customer_id` AS `customer`, `salesmanager_id` FROM `accounts`
WHERE `salesmanager_id` NOT IN (
SELECT `salesmanager_id` FROM `accounts` a
LEFT JOIN `customers` c ON (a.customer_id = c.id)
WHERE a.customer_id=c.id
) GROUP BY `customer`;

最佳答案

SELECT 
customer_id
FROM
accounts
GROUP BY
customer_id
HAVING
COUNT(DISTINCT salesmanager_id) > 1

这基本上获取了每个 customer_id 的所有 salesmanager_id,如果有多个唯一的 salesmanager_id 值,则 customer_id 被返回。

关于mysql - 在 MySQL 表中查找 "exotic"值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115590/

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