我做错了什么?如果更多的人单身而不是已婚,则需要显示是,如果反之,则不需要。我只想让它显示是或否。
IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';
答案 0 :(得分:2)
考虑到MySQL使用:
IF expression THEN
expression
ELSE
ENDIF;
你最好不要使用select语句
DECLARE married int, single int
SET married = select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = 'M'
SET single = select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = 'S'
IF married < single THEN
PRINT 'YES'
ELSE
PRINT 'NO'
END IF
回答是的,你的语法错了 https://dev.mysql.com/doc/refman/5.7/en/if.html
答案 1 :(得分:2)
你想要这样的东西:
select case when married >= single then 'M' else 'S' end
from
(
select sum(case when StudMaritalStatus = 'M' then 1 else 0 end) married
, sum (case when StudMaritalStatus = 'S' then 1 else 0 end) single
from students
) derivedTable
打破交易取决于您的业务需求,我们都不知道。