简体   繁体   English

Mysql 为不同的 where 子句显示不同的列

[英]Mysql show different columns for different where clauses

I want to show a output table that counts all the users found in a table.我想显示一个 output 表,该表计算在表中找到的所有用户。 Basically I want the output to look like:基本上我希望 output 看起来像:

+-----------+-----------+
| user1     | user2     |
+-----------+-----------+
|         5 |         2 |
+-----------+-----------+

I'm just using a dummy table to test this.我只是用一个虚拟表来测试这个。 My query looks like this:我的查询如下所示:

(
    select 
        name as user1 
    from 
        users 
    where 
        name = 'root'
) UNION (
    select 
        name as user2 
    from
        users 
    where 
        name = 'not_root'
)

Which only outputs something like this:它只输出这样的东西:

+-----------+
| user1     |
+-----------+
|         5 |
|         2 |
+-----------+

EDITED EDITED

The main idea of the approach it's treat a table as two different virtual tables in subquery. 该方法的主要思想是将表视为子查询中的两个不同的虚拟表。 We can make nested select statement eg (select count(*) as c from users where name = 'root') u1 MySql treats it as particular table named u1 with one row and one column named c . 我们可以创建嵌套的select语句,例如(select count(*) as c from users where name = 'root') u1 MySql将其视为名为u1特定表,其中一行包含一行c

select u1.c as root_cnt, u2.c as not_root_cnt
from (select count(*) as c from users where name = 'root') u1,
(select count(*) as c from users where name = 'not_root') u2

or 要么

Moreover if you have select statement that returns only one row you can put nested selects directly in fields list 此外,如果您的select语句仅返回一行,则可以将嵌套的selects直接放入字段列表中

select (select count(*) as c from users where name = 'root') as root_cnt, (select count(*) as c from users where name = 'not_root') as not_root_cnt

Definite disadvantage of such approach it's extra subqueries. 这种方法的绝对缺点是额外的子查询。 Method based on using case when construction free from such disadvantage. 基于在没有这种缺点的case when使用case when方法。

Try this 尝试这个

SELECT
    SUM(CASE WHEN Name = 'root' THEN 1 ELSE 0 END) user1,
    SUM(CASE WHEN Name = 'not_root' THEN 1 ELSE 0 END) user2
FROM Users

You can use a case statement inside of count to get the counts in separate columns 您可以在count内使用case语句以在单独的列中获取计数

select
    count(case when name = 'root' then 1 end) user1,
    count(case when name = 'not_root' then 1 end) user2
from users
where name in ('root','not_root')

It seems your query is wrong.. 看来您的查询是错误的。

union won't combine the results of two queries in the way you have described above. union不会以您上面描述的方式合并两个查询的结果。

union would combine the result of two or more select statements but wouldn't "join" it. union将合并两个或多个select语句的结果,但不会“联接”它。

You might want to use joins for this cause. 您可能要为此使用联接。 still you wouldn't be able to put 5|2 in same row as it basically suggests --> fetch user 1 and user 2 values for one particular type 仍然无法像基本建议那样将5 | 2放在同一行中->获取一种特定类型的用户1和用户2值

i think group by is a much better approach: select user, count(user) from users group by user我认为 group by 是一种更好的方法: select user, count(user) from users group by user

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

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