我试图在 table_Y 中找到拥有不同数量标题的客户总数,并将它们按 table_X 中的 marital_status 分组。我无法加入他们,因为我没有加入他们的唯一密钥。我正在编写以下 mySQL 脚本,该脚本需要很长时间。有没有更好的方法来写这个?谢谢:
SELECT COUNT(DISTINCT customer_id), marital_status FROM table_X
WHERE customer_id IN (
SELECT COUNT(DISTINCT customer_id)
FROM
(
SELECT customer_id,COUNT(DISTINCT title)
FROM table_Y
GROUP BY customer_id
HAVING COUNT(DISTINCT title)=1
ORDER BY customer_id) as t1)
GROUP BY marital_status;