if语句中的mysql多个条件

时间:2014-05-20 13:17:34

标签: mysql

mysql的新手,所以我不确定我是否正确地提出了这个问题。我试图将多个参数添加到if语句。我试图检查两队之间的比赛是否是联赛。

SELECT visitor AS school, home AS temp, vl.leagueid AS vleague, hl.leagueid AS hleague, 
if(vl.leagueid = hl.leagueid, 1, 0) AS leaguematch
FROM schedule 
LEFT JOIN schools AS vl ON vl.id = visitor 
LEFT JOIN schools AS hl ON hl.id = home
WHERE gamedate between '2013-01-01' AND '2013-12-31'

检查联盟是否相等我想加((vl.leagueid = 26 AND hl.leagueid = 27)OR(vl.leagueid = 27 AND hl.leagueid = 26))。我试过的()的组合似乎没有用,所以我觉得我错了。

谢谢, 麦克

感谢chofer,这是我的工作查询

SELECT visitor AS school, home AS temp, vl.leagueid AS vleague, hl.leagueid AS hleague, 
CASE 
    WHEN vl.leagueid = hl.leagueid THEN '1' 
    WHEN ((vl.leagueid = 26 AND hl.leagueid = 27) OR (vl.leagueid = 27 AND hl.leagueid = 26)) THEN '1' 
    ELSE 0 
END AS leaguematch

FROM u96nk_rvball_schedule 
LEFT JOIN u96nk_rvball_schools AS vl ON vl.id = visitor 
LEFT JOIN u96nk_rvball_schools AS hl ON hl.id = home
WHERE gamedate between '2013-01-01' AND '2013-12-31'

1 个答案:

答案 0 :(得分:3)

如果你需要vl.leagueid和hl.leagueid相等就把条件放在哪里,就像这样

WHERE gamedate between '2013-01-01' AND '2013-12-31' AND vl.leagueid = hl.leagueid

EDIT  如果你想在符合条件的情况下将联赛匹配设置为特定值,如果不是CASE则为0将是最佳选择

SELECT visitor AS school,
       home AS temp,
       vl.leagueid AS vleague,
       hl.leagueid AS hleague,
       CASE 
          WHEN school = 50 AND temp = 2 THEN '1' 
          WHEN school = 51 AND temp = 3 THEN '2' 
          ELSE 0 
       END AS leaguematch
FROM ......