简体   繁体   English

从两个表中选择计数不同值

[英]Select Count Distinct values from two tables

Users Table: 用户表:

+-----------+----------+------------+-------------+
| name      | fb_id    | date       | flipbook    |
+-----------+----------+------------+-------------+
| Tia       | 66783722 | 1975-09-18 |  june 2014  |
| Nikki     | 10438259 | 1972-03-04 |  july 2014  |
| Yamila    | 73370629 | 1972-03-04 | august 2014 | 
+-----------+----------+------------+-------------+

Visits Table: 访问表:

+-----------+----------+------------+-------------+
| Name      | fb_id    | Date       |  Flipbook   |
+-----------+----------+------------+-------------+
| Tia       | 66783722 | 1975-09-18 | june 2014   |
| Nikki     | 10438259 | 1972-03-04 | august 2014 |
| Nikki     | 10438259 | 1972-04-04 | october 2014|
+-----------+----------+------------+-------------+

I want the query to return all users from from user Table and count the number of flipbooks, for example: 我希望查询从用户表返回所有用户,并计算活动簿的数量,例如:

[1]
name => Tia, 
fb_id = 66783722,
date => 1975-09-18,
count_flip => 1 (june 2014 is in both tables so we count it as 1)

[2]
name => Nikki, 
fb_id = 10438259,
date => 1972-03-04,
count_flip => 3 (because in the first table we have june 2014 and in the second table we have august 2014 and october 2014, so no duplicates)

[3]
name => Yamila, 
fb_id = 73370629,
date => 1972-03-04,
count_flip => 1 (because we have august 2014 in the first table and she is not mentioned in the second one)

I tryed to do this query: 我试图做这个查询:

SELECT u.*, (SELECT COUNT(Distinct flipbook) FROM visits v WHERE v.fb_id = u.fb_id) as count_flip FROM users u

But the problem is I am missing the users that are in the users table but not in the visits table. 但是问题是我缺少了用户表中的用户,但是缺少访问表中的用户。 So for the example above I wouldn't see "Yamila" in my query. 因此,对于上面的示例,我在查询中不会看到“ Yamila”。

Any thoughts how to fix this? 有什么想法如何解决这个问题?

Another way of thinking about the logic is that you want to count 1 for the users table and then add up all non-matching flipbook values for the name in the visits table. 关于逻辑的另一种思考方式是,您希望对users表计数1,然后将visits表中name所有不匹配的活页簿值相加。 This suggests a correlated subquery: 这表明相关的子查询:

select u.*,
       (select 1 + count(*)
        from visits v
        where v.name = u.name and v.flipbook <> u.flipbook
       ) as nr
from users u;

EDIT: 编辑:

Another way to do what you want is less efficient (assuming you have the right indexes for the above query): 另一种执行所需操作的方法效率较低(假设您为上述查询使用了正确的索引):

select u.*, uv.nr
from users u join
     (select u.name, count(distinct flipbook) as nr
      from ((select name, flipbook from users) union all
            (select name, flipbook from visits)
           ) uv
      group by u.name
     ) uv
     on u.name = uv.name;

I think you can just do this:- 我想你可以做到这一点:

SELECT users.name, users.fb_id, users.date, COUNT(DISTINCT visits.Flipbook) + 1 AS count_flip
FROM users
LEFT OUTER JOIN visits
ON users.fb_id = visits.fb_id
AND users.flipbook != visits.flipbook
GROUP BY users.name, users.fb_id, users.date

Does a LEFT OUTER JOIN to get a row for each matching user with a different date. 是否进行LEFT OUTER JOIN来为每个具有不同日期的匹配用户获得一行。 Then just ad one to the resulting count (to count the original field on the users table). 然后,仅对结果计数加一(以对用户表中的原始字段进行计数)。

This avoids any sub queries. 这样可以避免任何子查询。

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

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