Mysql在末尾添加一个额外的列而不是使用union

时间:2017-12-01 01:06:07

标签: mysql

我正在尝试组合两个查询,以便数据显示在一个表中。我正在使用一个联合来组合这两个查询。但是,所有内容都会添加到同一列中,我会更改哪些内容,以便不同查询的结果占用新列。 以下是查询结果的图像。enter image description here

这是我的代码

select * from(

SELECT
CASE
    WHEN age BETWEEN 18 and 25 THEN 'Under 25'
    WHEN age BETWEEN 25 and 40 THEN '25 - 40'
    WHEN age >= 40 THEN 'Over 40'
    WHEN age IS NULL THEN 'Not Filled In (NULL)'
END as age_range,
COUNT(*) AS count,
 CASE
    WHEN age between 18 and 25 THEN 1
    WHEN age BETWEEN 25 and 40 THEN 2
    WHEN age >= 40 THEN 8
    WHEN age IS NULL THEN 9
END as ordinal

FROM (SELECT TIMESTAMPDIFF(YEAR, users.birthdate_on, CURDATE()) AS age FROM users
join subscriptions on users.id = subscriptions.user_id
where users.plan <> 'domain' and users.plan <> '' and users.plan <> 'domain_cpi' and users.birthdate_on is not null 
) as derived

GROUP BY age_range


union 

SELECT
CASE
    WHEN age BETWEEN 18 and 25 THEN 'Under 25'
    WHEN age BETWEEN 25 and 40 THEN '25 - 40'
    WHEN age >= 40 THEN 'Over 40'
    WHEN age IS NULL THEN 'Not Filled In (NULL)'
END as age_range2,
COUNT(*) AS count2,
 CASE
    WHEN age between 18 and 25 THEN 1
    WHEN age BETWEEN 25 and 40 THEN 2
    WHEN age >= 40 THEN 8
    WHEN age IS NULL THEN 9
END as ordinal

FROM (SELECT TIMESTAMPDIFF(YEAR, users.birthdate_on, CURDATE()) AS age FROM users) as derived2

GROUP BY age_range2
) as test2




ORDER BY ordinal

我希望结果只有一个低于25的节目,但25 493和2046以下的两个结果在不同的列中。所有其他范围相同

2 个答案:

答案 0 :(得分:1)

听起来你想把一个JOIN放到derived.age_range on test2.age_range2

SELECT
CASE
    WHEN age BETWEEN 18 and 25 THEN 'Under 25'
    WHEN age BETWEEN 25 and 40 THEN '25 - 40'
    WHEN age >= 40 THEN 'Over 40'
    WHEN age IS NULL THEN 'Not Filled In (NULL)'
END as age_range, 
CASE
    WHEN age between 18 and 25 THEN 1
    WHEN age BETWEEN 25 and 40 THEN 2
    WHEN age >= 40 THEN 8
    WHEN age IS NULL THEN 9
    END as ordinal,
    count, count2
FROM (
    SELECT
    derived.age,
    COUNT(*) AS count
    FROM (
        SELECT TIMESTAMPDIFF(YEAR, users.birthdate_on, CURDATE()) AS age FROM users
        join subscriptions on users.id = subscriptions.user_id
        where users.plan <> 'domain' and users.plan <> '' and users.plan <> 'domain_cpi' and users.birthdate_on is not null 
        GROUP BY age
    ) as derived

    JOIN

    SELECT
    derived2.age,
    COUNT(*) AS count2
    FROM (
        SELECT TIMESTAMPDIFF(YEAR, users.birthdate_on, CURDATE()) AS age FROM users
        GROUP BY age
    ) as derived2

    ON derived.age = derived2.age

)
ORDER BY ordinal ASC;

答案 1 :(得分:0)

我认为你只需要左连接就需要2个查询。 count()函数仅对非空值递增,因此即使用户不符合订阅条件,也可以对其进行计数。

SELECT
      CASE
          WHEN age BETWEEN 18 and 25 THEN 'Under 25'
          WHEN age BETWEEN 25 and 40 THEN '25 - 40'
          WHEN age >= 40 THEN 'Over 40'
          WHEN age IS NULL THEN 'Not Filled In (NULL)'
      END as age_range 
    , CASE
          WHEN age between 18 and 25 THEN 1
          WHEN age BETWEEN 25 and 40 THEN 2
          WHEN age >= 40 THEN 8
          WHEN age IS NULL THEN 9
      END as ordinal
    , COUNT(DISTINCT id)    AS user_count        # distinct might not be needed
    , COUNT(subscriber_id)  AS subscriber_count
FROM (
      SELECT
            users.id
          , TIMESTAMPDIFF(YEAR, users.birthdate_on, CURDATE()) AS age
          , subscriptions.user_id                              AS subscriber_id
      FROM users
      LEFT JOIN subscriptions ON users.id = subscriptions.user_id
            AND users.plan <> 'domain'
            AND users.plan <> ''
            AND users.plan <> 'domain_cpi'
            AND users.birthdate_on IS NOT NULL
      ) d
GROUP BY
      CASE
          WHEN age BETWEEN 18 and 25 THEN 'Under 25'
          WHEN age BETWEEN 25 and 40 THEN '25 - 40'
          WHEN age >= 40 THEN 'Over 40'
          WHEN age IS NULL THEN 'Not Filled In (NULL)'
      END
    , CASE
          WHEN age between 18 and 25 THEN 1
          WHEN age BETWEEN 25 and 40 THEN 2
          WHEN age >= 40 THEN 8
          WHEN age IS NULL THEN 9
      END