垂直列结果到水平
Vertical column result to horizontal
我需要将列结果置于水平位置,就像 SUM(CASE WHEN status = 'On Hold' THEN 1 ELSE 0 END) AS 'word'
那样,我也尝试了子查询和别名 AS,但没有成功。我认为 JOIN 是针对多个表,而 UNION 是为了在同一列中获得结果。
我将此页面用作参考
https://www.mysqltutorial.org/mysql-case-function/
mysql Table: phpvms_pireps
pilotid|flightnum|submitdate|accepted
我的sql代码:
SELECT DISTINCT `phpvms_pireps`.`pilotid`
, `phpvms_pireps`.`accepted`
, `phpvms_pireps`.`flightnum`
, `phpvms_pireps`.`submitdate`
FROM phpvms_pireps
WHERE ((`phpvms_pireps`.`flightnum` in ('A-1', 'A-2', 'A-3', 'A-4')))
AND submitdate BETWEEN '2020-04-09' AND '2020-04-11'
ORDER
BY `phpvms_pireps`.`pilotid` ASC
结果:
我想实现这个:
我正在使用 php/html,如何获得该结果?
非常感谢!
您正在描述一个支点table。考虑使用条件聚合:
select
pilotid,
max(case when flightnum = 'A-1' then accepted end) a1,
max(case when flightnum = 'A-2' then accepted end) a2,
max(case when flightnum = 'A-3' then accepted end) a3,
max(case when flightnum = 'A-4' then accepted end) a4
from phpvms_pireps
where
flightnum in ('A-1', 'A-2', 'A-3', 'A-4')
and submitdate between '2020-04-09' and '2020-04-11'
group by pilotid
order by pilotid
我需要将列结果置于水平位置,就像 SUM(CASE WHEN status = 'On Hold' THEN 1 ELSE 0 END) AS 'word'
那样,我也尝试了子查询和别名 AS,但没有成功。我认为 JOIN 是针对多个表,而 UNION 是为了在同一列中获得结果。
我将此页面用作参考
https://www.mysqltutorial.org/mysql-case-function/
mysql Table: phpvms_pireps
pilotid|flightnum|submitdate|accepted
我的sql代码:
SELECT DISTINCT `phpvms_pireps`.`pilotid`
, `phpvms_pireps`.`accepted`
, `phpvms_pireps`.`flightnum`
, `phpvms_pireps`.`submitdate`
FROM phpvms_pireps
WHERE ((`phpvms_pireps`.`flightnum` in ('A-1', 'A-2', 'A-3', 'A-4')))
AND submitdate BETWEEN '2020-04-09' AND '2020-04-11'
ORDER
BY `phpvms_pireps`.`pilotid` ASC
结果:
我想实现这个:
我正在使用 php/html,如何获得该结果?
非常感谢!
您正在描述一个支点table。考虑使用条件聚合:
select
pilotid,
max(case when flightnum = 'A-1' then accepted end) a1,
max(case when flightnum = 'A-2' then accepted end) a2,
max(case when flightnum = 'A-3' then accepted end) a3,
max(case when flightnum = 'A-4' then accepted end) a4
from phpvms_pireps
where
flightnum in ('A-1', 'A-2', 'A-3', 'A-4')
and submitdate between '2020-04-09' and '2020-04-11'
group by pilotid
order by pilotid