gpt4 book ai didi

MySQL 行输出单元格到列中

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

我的问题是如何将行单元格输出放入单独的列中,以提高查看数据的可读性。

我有以下 SQL 查询:

SELECT TD.name AS Conditions, PV.value AS Frequency, MSL.name AS  
Mailing_List_Subscriptions, Count(CI.uid) AS Users_Signup_Count
FROM conditions_interest CI
INNER JOIN profile_values PV
ON CI.uid = PV.uid
INNER JOIN hwmailservice_user_lists MSUL
ON CI.uid = MSUL.uid
INNER JOIN hwmailservice_lists MSL
ON MSUL.list_id = MSL.list_id
INNER JOIN term_data TD
ON CI.tid = TD.tid
WHERE (PV.value = 'daily' OR PV.value = 'weekly') AND CI.email = '1'
GROUP BY PV.value, TD.name, MSL.name
ORDER BY TD.name;

输出如下:

SQL OUTPUT

因此所有邮件列表订阅都将有自己的单独列,其中包含与条件关联的计数。所以像这样:

Conditions Frequency Newsletter Partners Annoucements marketing
Abscessed Tooth Daily 95 91 98 98
Abscessed Tooth Weekly 6 4 7 7

如果需要更多说明,我将编辑我的帖子。

最佳答案

MySQL 没有你正在做的 PIVOT 函数,所以你需要使用 CASE:

SELECT x.Conditions,
x.Frequency,
SUM(CASE WHEN Mailing_List_Subscriptions = 'newsletter' THEN Users_Signup_Count END) newsletter,
SUM(CASE WHEN Mailing_List_Subscriptions = 'partners' THEN Users_Signup_Count END) partners,
SUM(CASE WHEN Mailing_List_Subscriptions = 'announcements' THEN Users_Signup_Count END) announcements,
SUM(CASE WHEN Mailing_List_Subscriptions = 'marketing' THEN Users_Signup_Count END) marketing
FROM
(
SELECT TD.name AS Conditions, PV.value AS Frequency,
MSL.name AS Mailing_List_Subscriptions,
Count(CI.uid) AS Users_Signup_Count
FROM conditions_interest CI
INNER JOIN profile_values PV
ON CI.uid = PV.uid
INNER JOIN hwmailservice_user_lists MSUL
ON CI.uid = MSUL.uid
INNER JOIN hwmailservice_lists MSL
ON MSUL.list_id = MSL.list_id
INNER JOIN term_data TD
ON CI.tid = TD.tid
WHERE (PV.value = 'daily' OR PV.value = 'weekly') AND CI.email = '1'
GROUP BY PV.value, TD.name, MSL.name
) x
GROUP BY x.Conditions, x.Frequency
ORDER BY x.name

关于MySQL 行输出单元格到列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11800716/

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