简体   繁体   English

MYSQL将两个表与两个列进行比较

[英]MYSQL join two tables comparing with two columns

I have two tables 1)users(id,registerdate) 2)user_answer(userid,answer,updated_date) 我有两个表1)users(id,registerdate)2)user_answer(userid,answer,updated_date)

I want the count of zero usage per day. 我想要每天零使用的计数。 How many users are registering but not answering per day. 每天有多少用户正在注册,但没有答复。 Results will be like this: 结果将如下所示:

Date        registedCount   notAnsweredCount

15-09-02    20              10
15-09-01    20              10
15-08-31    12              4

Thanks in advance. 提前致谢。

Using a SUM of the result of an IF statement:- 使用IF语句的结果的总和:

SELECT date_range.aDay, 
        COUNT(DISTINCT users.id) AS registedCount, 
        SUM(IF(users.id IS NOT NULL AND user_answer.userid IS NULL, 1, 0)) AS notAnsweredCount
FROM
(
    SELECT DATE_ADD('2015-09-01', INTERVAL units.aCnt + tens.aCnt * 10 DAY) AS aDay
    FROM
    (
        SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
    ) units
    CROSS JOIN
    (
        SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
    ) tens
) date_range
LEFT OUTER JOIN users
ON date_range.aDay = users.registerdate
LEFT OUTER JOIN user_answer
ON users.id = user_answer.userid
GROUP BY date_range.aDay

This is getting a range of numbers to add to a date, to give a range of dates (in this case 100 days from 1st September inclusive). 这将获得一系列要添加到日期中的数字,以提供日期范围(在这种情况下,自9月1日起,包括100天)。 This is then LEFT OUTER JOINed against users matching on the register date, and then that is LEFT OUTER JOINed against user_answer. 然后针对注册日期匹配的用户进行LEFT OUTER JOINed,然后针对user_answer进行LEFT OUTER JOINed。 It does a count of distinct user.id to get the number of people who registered on each date, and it sums up where the userids from user_answer is NULL (ie, haven't answered any question). 它对不同的user.id进行计数,以获取每个日期的注册人数,并且将user_answer中的userid汇总为NULL(即,未回答任何问题)。

EDIT - if you do not care about having gaps in the returned data for days where no user registered, and want a list of all days where at least 1 person registered this this simple query will do the job:- 编辑-如果您不关心没有用户注册的日子里返回的数据中有空白,并且想要一个至少有1个人注册的日子的清单,这个简单的查询就可以完成:

SELECT users.registerdate, 
        COUNT(DISTINCT users.id) AS registedCount, 
        SUM(IF(user_answer.userid IS NULL, 1, 0)) AS notAnsweredCount
FROM users
LEFT OUTER JOIN user_answer
ON users.id = user_answer.userid
GROUP BY users.registerdate

Check the following query, it should get you the desired results 检查以下查询,它将为您提供所需的结果

SELECT results.date, 
    SUM(results.answered) as answered,
    COUNT(results.userid)-SUM(results.answered) as notanswered
FROM
(SELECT @id:= u.id as userid, @regdate:= u.regdate as date,
   (CASE WHEN EXISTS (SELECT 1 FROM user_answer WHERE updated_date = @regdate AND userid = @id)
    THEN 1 ELSE 0 END) AS answered
FROM users u) AS results
GROUP BY results.date

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

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