MySQL:查询根据另一列的属性对数据进行排序

发布于 2024-09-15 11:23:14 字数 321 浏览 6 评论 0原文

因此,为此想出一个标题很困难

SELECT Date(p.born_at), SUM(p.value) as 'total_value_for_day'
FROM puppies as p
WHERE p.status = 2
GROUP BY DATE(p.born_at);

因此,这给出了 2 列结果。

我想要的是这样的:

列:

date | status = 1 | status = 2 | status = 3
DATA!!!

有办法做到这一点吗?

So, coming up with a title for this is difficult

SELECT Date(p.born_at), SUM(p.value) as 'total_value_for_day'
FROM puppies as p
WHERE p.status = 2
GROUP BY DATE(p.born_at);

So, this gives a 2 column result.

What I want is this:

columns:

date | status = 1 | status = 2 | status = 3
DATA!!!

is there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一腔孤↑勇 2024-09-22 11:23:14
SELECT Date(p.born_at), 
SUM(case when status = 1 then p.value end) as 'status = 1',
SUM(case when status = 2 then p.value end) as 'status = 2',
SUM(case when status = 3 then p.value end) as 'status = 3'
FROM puppies as p
WHERE p.status in(1,2,3)
GROUP BY DATE(p.born_at);
SELECT Date(p.born_at), 
SUM(case when status = 1 then p.value end) as 'status = 1',
SUM(case when status = 2 then p.value end) as 'status = 2',
SUM(case when status = 3 then p.value end) as 'status = 3'
FROM puppies as p
WHERE p.status in(1,2,3)
GROUP BY DATE(p.born_at);
绮烟 2024-09-22 11:23:14
SELECT Date(born_at), 
    SUM(case when Status = 1 then value else 0 end) as 'status = 1',
    SUM(case when Status = 2 then value else 0 end) as 'status = 2',
    SUM(case when Status = 3 then value else 0 end) as 'status = 3' 
FROM puppies 
GROUP BY DATE(born_at); 
SELECT Date(born_at), 
    SUM(case when Status = 1 then value else 0 end) as 'status = 1',
    SUM(case when Status = 2 then value else 0 end) as 'status = 2',
    SUM(case when Status = 3 then value else 0 end) as 'status = 3' 
FROM puppies 
GROUP BY DATE(born_at); 
枯叶蝶 2024-09-22 11:23:14
SELECT
   DATE(p.born_at)
   , SUM(CASE WHEN STATUS = 1 THEN p.value END) AS 'Status = 1'
   , SUM(CASE WHEN STATUS = 2 THEN p.value END) AS 'Status = 2'
   , SUM(CASE WHEN STATUS = 3 THEN p.value END) AS 'Status = 3'
FROM puppies AS p
WHERE p.status IN(1, 2, 3)
GROUP BY DATE(p.born_at);
SELECT
   DATE(p.born_at)
   , SUM(CASE WHEN STATUS = 1 THEN p.value END) AS 'Status = 1'
   , SUM(CASE WHEN STATUS = 2 THEN p.value END) AS 'Status = 2'
   , SUM(CASE WHEN STATUS = 3 THEN p.value END) AS 'Status = 3'
FROM puppies AS p
WHERE p.status IN(1, 2, 3)
GROUP BY DATE(p.born_at);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文