gpt4 book ai didi

MySQL-显示输出列一个在另一个下面,并且标题与列名不同

转载 作者:行者123 更新时间:2023-11-29 12:01:24 25 4
gpt4 key购买 nike

当我在 mySQL 上运行以下 SQL 时

SELECT t1.U_COUNT AS U_COUNT, t2.R_COUNT AS R_COUNT 
FROM
(
SELECT SUM(TRP_COUNT) AS U_COUNT
FROM `SAL_TAB`
WHERE CUST='SS' AND TV_POS='VFG'
) t1,
(
SELECT SUM(TRP_COUNT) AS R_COUNT
FROM `SAL_TAB`
WHERE CUST='SS' AND TV_POS='FHT'
) t2;

我得到的输出是

+---------+---------+
| U_COUNT | R_COUNT |
+---------+---------+
| 56 | 67 |
+---------+---------+

我想要的输出是,

+------------+-------------+
| SALES_TYPE | SALES_COUNT |
+------------+-------------+
| U_COUNT | 56 |
| R_COUNT | 67 |
+------------+-------------+

我现在花了很多时间试图解决这个问题,但一直没能成功。有人可以帮我吗?

最佳答案

试试这个

SELECT 'U_COUNT' as SALES_TYPE,SUM(TRP_COUNT) AS SALES_COUNT 
FROM `SAL_TAB`
WHERE CUST='SS' AND TV_POS='VFG'
UNION ALL
SELECT 'R_COUNT' as SALES_TYPE,SUM(TRP_COUNT) AS SALES_COUNT
FROM `SAL_TAB`
WHERE CUST='SS' AND TV_POS='FHT'

关于MySQL-显示输出列一个在另一个下面,并且标题与列名不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308794/

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