比较一行中的两个值

ufj5ltwl  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(281)
select event_slots.id as event_slot_id, u.first_name, u.id AS user_id,

sum(case when next_round = 1 then 1 else 0 end) AS yes,
sum(case when next_round = 2 then 1 else 0 end) AS no

from event_slots

left join users as u on u.id = event_slots.selected_by_user_id 
left join audition_card_values as acv on acv.user_id = event_slots.selected_by_user_id 

where 
event_id = 1 and 
selected_by_user_id is not null

group by acv.user_id

ergebnis公司:

现在我只需要第一行“first\u name”=“eins”,因为“yes”等于“no”。我怎样才能使这个地方变得庄重?其中yes=no。。。不起作用。
谢谢你的帮助

cnh2zyt3

cnh2zyt31#

我想把这个问题表述为:

select 
    ev.id as event_slot_id, 
    u.first_name, 
    u.id AS user_id,
    sum(next_round = 1) AS yes,
    sum(next_round = 2) AS no
from event_slots as ev
inner join audition_card_values as acv 
    on acv.user_id = ev.selected_by_user_id 
left join users as u 
    on u.id = ev.selected_by_user_id 
where e.event_id = 1
group by ev.id, u.first_name, u.id
having sum(next_round = 1) = sum(next_round = 2)

理论基础:
中的条件表达式 sum() 可以简化
你可以使用 having 过滤条款;mysql还支持重用 select 合同条款 having 从句,所以你可以拼这个: having yes = no -但是我更喜欢重复条件表达式,因为这是标准的sql
这个 left joinaudition_card_values 接着是一个 where ... is not null 关于连接列可以重写为一个简单的 inner join 我把你的头发修好了 group by 条款;中的每个非聚合列 select 子句必须出现在 group by 条款

相关问题