MySQL - 取决于二进制标准的条件打印结果
MySQL - Conditionnal printing results depending on binary criteria
我正在寻找一种方法,以某种方式从我的 sql 查询中获取结果。
抱歉,标题不好,如果您有任何建议可以更好地解释我的问题,请告诉我,我会更改它。
用简单的词来解释有点困难,所以让我告诉你我有什么以及我想得到什么:
这是我正在处理的数据库
它由脚本填充,在打印机服务器上寻找任何打印查询
数据库结构
我的查询在这里:
SELECT SUM(pages*copies) as "nbpages", user, Grayscale
FROM presentation
WHERE Grayscale NOT LIKE ""
GROUP BY user, Grayscale
ORDER BY `user` ASC, grayscale ASC;
WHERE 子句在这里只有两个选项:“GRAYSCALE”或“NOT GRAYSCALE”
这是结果
nbpages
user
grayscale
654
user1
GRAYSCALE
5689
user1
NOT GRAYSCALE
856
user2
GRAYSCALE
45
user3
GRAYSCALE
30
user3
NOT GRAYSCALE
我想得到类似的东西:
user
nbgray
nbnogray
user1
654
5689
user2
856
0
user3
45
30
有什么建议吗?
在我看来像是一个简单的条件聚合:
select
user,
sum (case when grayscale = 'GRAYSCALE' then pages*copies else 0 end) as nbgray,
sum(case when grayscale <> 'GRAYSCALE' then pages*copieselse 0 end) as nbnogray
from
<table>
group by
user
使用条件聚合:
SELECT user,
SUM(CASE WHEN Grayscale = 'GRAYSCALE'
THEN pages*copies ELSE 0 END) AS nbgray,
SUM(CASE WHEN Grayscale = 'NOT GRAYSCALE'
THEN pages*copies ELSE 0 END) AS nbnogray
FROM presentation
GROUP BY user
ORDER BY user;
我正在寻找一种方法,以某种方式从我的 sql 查询中获取结果。
抱歉,标题不好,如果您有任何建议可以更好地解释我的问题,请告诉我,我会更改它。 用简单的词来解释有点困难,所以让我告诉你我有什么以及我想得到什么:
这是我正在处理的数据库 它由脚本填充,在打印机服务器上寻找任何打印查询
数据库结构
我的查询在这里:
SELECT SUM(pages*copies) as "nbpages", user, Grayscale
FROM presentation
WHERE Grayscale NOT LIKE ""
GROUP BY user, Grayscale
ORDER BY `user` ASC, grayscale ASC;
WHERE 子句在这里只有两个选项:“GRAYSCALE”或“NOT GRAYSCALE”
这是结果
nbpages | user | grayscale |
---|---|---|
654 | user1 | GRAYSCALE |
5689 | user1 | NOT GRAYSCALE |
856 | user2 | GRAYSCALE |
45 | user3 | GRAYSCALE |
30 | user3 | NOT GRAYSCALE |
我想得到类似的东西:
user | nbgray | nbnogray |
---|---|---|
user1 | 654 | 5689 |
user2 | 856 | 0 |
user3 | 45 | 30 |
有什么建议吗?
在我看来像是一个简单的条件聚合:
select
user,
sum (case when grayscale = 'GRAYSCALE' then pages*copies else 0 end) as nbgray,
sum(case when grayscale <> 'GRAYSCALE' then pages*copieselse 0 end) as nbnogray
from
<table>
group by
user
使用条件聚合:
SELECT user,
SUM(CASE WHEN Grayscale = 'GRAYSCALE'
THEN pages*copies ELSE 0 END) AS nbgray,
SUM(CASE WHEN Grayscale = 'NOT GRAYSCALE'
THEN pages*copies ELSE 0 END) AS nbnogray
FROM presentation
GROUP BY user
ORDER BY user;