简体   繁体   English

错误使用内部联接功能/组功能?

[英]Wrong use of inner join function / group function?

I have the following problem with my query: 我的查询存在以下问题:

I have two tables: 我有两个表:

  1. Customer 顾客
  2. Subscriber 订户

linked together by customer.id=subscriber.customer_id 由customer.id = subscriber.customer_id链接在一起

in the subscriber table, I have records with id_customer=0 (these are email records, that do not have a full customer account) 在订户表中,我有id_customer = 0的记录(这些是电子邮件记录,没有完整的客户帐户)

Now i want to show how many customers I have per day, and how many subscribers with id_customer, and how many subscribers WITH id_customer=0 (emailonlies i call them) 现在,我想显示我每天有多少客户,有id_customer的订阅者有多少,以及有id_customer = 0的订阅者有多少(电子邮件,我打电话给他们)

Somehow, i cannot manage to get those emailonlies. 不知何故,我无法获得那些电子邮件。 Perhaps it has something to do with not using the right join type. 也许与不使用正确的联接类型有关。 When i use left join, i get the right amount of customers, but not the right amount of emailonlies. 当我使用左联接时,我得到的客户数量合适,但电子邮件数量却不正确。 When I use inner join i get the wrong amount of customers. 当我使用内部联接时,我得到的客户数量错误。 Am i using the group function correctly? 我是否正确使用分组功能? i think it has something to do with that. 我认为这与此有关。

THIS IS MY QUERY: 这是我的查询:

   `   SELECT DATE(c.date_register),
COUNT(DISTINCT c.id) AS newcustomers,
COUNT(DISTINCT s.customer_id) AS newsubscribedcustomers,
COUNT(DISTINCT s.subscriber_id AND s.customer_id=0) AS emailonlies
FROM customer c
LEFT JOIN subscriber s ON s.customer_id=c.id
GROUP BY DATE(c.date_register)
ORDER BY DATE(c.date_register) DESC
LIMIT 10
;`

I'm not entirely sure, but I think in DISTINCT s.subscriber_id AND s.customer_id=0 , it runs the AND before the DISTINCT , so the DISTINCT only ever sees true and false. 我不确定,但我认为在DISTINCT s.subscriber_id AND s.customer_id=0 ,它在DISTINCT之前运行AND ,因此DISTINCT只会看到DISTINCT s.subscriber_id AND s.customer_id=0

Why don't you just take 你为什么不随便拿

COUNT(DISTINCT s.subscriber_id) - (COUNT(DISTINCT s.customer_id) - 1)?

(The -1 is there because DISTINCT s.customer_id will count 0.) (-1存在,因为DISTINCT s.customer_id将计数为0。)

Got it, only risk is that i get no email onlies if there are no customers on this day, becuase of the left join. 知道了,唯一的风险是如果这一天没有客户,那么我就不会收到电子邮件,这是因为左加入。 But this one works: 但这一项有效:

SELECT customers.regdatum,customers.customersqty,subscribers.emailonlies 选择customer.regdatum,customers.customersqty,subscribers.emailonlies

FROM ( (SELECT DATE(c.date_register) AS regdatum,COUNT(DISTINCT c.id) AS customersqty FROM customer c GROUP BY DATE(c.date_register) ) AS customers FROM((选择DATE(c.date_register)AS regdatum,COUNT(DISTINCT c.id)AS客户数量FROM客户c GROUP BY DATE(c.date_register))AS客户

LEFT JOIN 左联接

(SELECT DATE(s.added) AS voegdatum,COUNT(DISTINCT s.subscriber_id) AS emailonlies
FROM subscriber s
WHERE s.customer_id=0
GROUP BY DATE(s.added)
) AS subscribers

ON customers.regdatum=subscribers.voegdatum ) ORDER BY customers.regdatum DESC ; ON customer.regdatum = subscribers.voegdatum)ORDER BY客户.regdatum DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM