gpt4 book ai didi

mysql - 将 MYSQL 行值转换为列名

转载 作者:行者123 更新时间:2023-11-29 05:49:40 28 4
gpt4 key购买 nike

我有这样的表:

job_status_air

status_id  | status
-----------+------------
1 | AIRPORT IN
2 | AIRPORT OUT

job_status_air_pkg

id  | status_id  | package_no
----+------------+------------
1 | 1 | pkg1
2 | 1 | pkg2
3 | 1 | pkg3
4 | 2 | pkg1

我正在使用以下查询:

select package_no , 
(case when job_status_air.status = "AIRPORT IN" then job_status_air_pkg.id end) AIRPORTIN,
(case when job_status_air.status = "AIRPORT OUT" then job_status_air_pkg.id else 0 end) AIRPORTOUT
FROM job_status_air_pkg
LEFT JOIN job_status_air ON (job_status_air.status_id = job_status_air_pkg.status_id)
WHERE job_status_air.mawb = 'awb1' or hawb = 'awb1'

我无法实现以下结果:

package_no | AIRPORT IN | AIRPORT OUT
-----------+------------+-------------
pkg1 | 1 | 4
pkg2 | 2 |
pkg3 | 3 |

有人能帮帮我吗?

最佳答案

你需要应用聚合和分组

select package_no , 
max(case when job_status_air.status = "AIRPORT IN" then job_status_air_pkg.id end) AIRPORTIN,
max(case when job_status_air.status = "AIRPORT OUT" then job_status_air_pkg.id else 0 end) AIRPORTOUT
FROM job_status_air_pkg
LEFT JOIN job_status_air ON (job_status_air.status_id = job_status_air_pkg.status_id)
WHERE job_status_air.mawb = 'awb1' or hawb = 'awb1'
group by package_no

关于mysql - 将 MYSQL 行值转换为列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com