gpt4 book ai didi

mysql - 如果在列表中,则选择更改列值

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

我正在尝试查询我的表来计算投票数,以及投票方法是否在列表中 ['C', 'M', 'S', 'L', 'T', 'V ', 'B', 'E'] 然后将其计为 1,并将 vote_method 替换为 'L'。

现在我有以下查询,它返回正确的结果,但不处理重复项。

select `election_lbl`, `voting_method`, count(*) as numVotes 
from `gen2014` group by `election_lbl`, `voting_method` order by `election_lbl` asc

election_lbl voting_method numVotes
2014-09-04 M 1
2014-09-05 M 2
2014-09-05 S 1
2014-09-08 C 16
2014-09-08 M 5
2014-09-08 S 9
2014-09-09 10 5
2014-09-09 C 46
2014-09-09 M 4
2014-09-09 S 5
2014-09-10 C 92
2014-0g-10 M 3
2014-09-10 S 7
2014-09-11 C 96
2014-09-11 M 3
2014-09-11 S 2
2014-09-12 C 104
2014-09-12 M 10
2014-09-12 S 3
2014-09-15 C 243
2014-09-15 M 18
2014-09-15 S 3
2014-09-16 10 1
2014-09-16 C 161
2014-09-16 M 4
2014-09-16 S 3
2014-09-17 C 157
2014-09-17 M 5
2014-09-17 S 12

您可以看到,对于2014-09-05,我有两个voting_method M和S,它们都在列表中。如果值在列表中,我希望理想的结果是删除重复的日期字段。因此,它将是 2014-09-05 'L' 3。我不希望对该日期的投票消失,因此结果应将它们算作 1。

将查询更改为此,但 mysql 说语法错误。

select `election_lbl`, `voting_method`, count(*) as numVotes from `gen2014`
(case `voting_method` when in ('C', 'M', 'S', 'L', 'T', 'V', 'B', 'E')
then 'L' END) group by `election_lbl`, `voting_method` order by `election_lbl` asc

表架构

CREATE TABLE `gen2014` (
`voting_method` varchar(255) DEFAULT NULL,
`election_lbl` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

最佳答案

SELECT election_lbl
, CASE WHEN voting_method IN ('C','M','S','L','T','V','B','E')
THEN 'L'
ELSE voting_method END my_voting_method
, COUNT(*)
FROM my_table
GROUP
BY my_voting_method -- or vice
, election_lbl; -- versa

关于mysql - 如果在列表中,则选择更改列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281617/

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