在MySQL中使用IF语句

时间:2012-11-14 15:03:21

标签: mysql sql if-statement

我有以下SQL语句。我想要做的是包括一个IF语句,以涵盖其中一列返回0或空白结果的可能性,没有0 ID。在这种情况下,我试图涵盖magez_cfv_nations结果可能返回零或空白结果的可能性。

SELECT c.clan_name, n.nation_name, 
    CONCAT(r.rarity_shorthand, " - ", r.rarity_name) AS rarity_text, 
    t.trigger_name, s.skill_name
 FROM `magez_cfv_cards` AS cards
 JOIN `magez_cfv_clans` c ON cards.clan_id = c.clan_id
 JOIN `magez_cfv_nations` n ON cards.nation_id = n.nation_id
 JOIN `magez_cfv_rarity` r ON cards.rarity_id = r.rarity_id
 JOIN `magez_cfv_trigger` t ON cards.trigger_id = t.trigger_id
 JOIN `magez_cfv_skills` s ON cards.skill_id = s.skill_id

2 个答案:

答案 0 :(得分:2)

您需要LEFT JOIN

  

LEFT JOIN关键字返回左表中的所有行   (table_name1),即使右表中没有匹配项

试试这个

SELECT c.clan_name, n.nation_name, 
    CONCAT(r.rarity_shorthand, " - ", r.rarity_name) AS rarity_text, 
    t.trigger_name, s.skill_name
 FROM `magez_cfv_cards` AS cards
 JOIN `magez_cfv_clans` c ON cards.clan_id = c.clan_id
 LEFT JOIN `magez_cfv_nations` n ON cards.nation_id = n.nation_id
 JOIN `magez_cfv_rarity` r ON cards.rarity_id = r.rarity_id
 JOIN `magez_cfv_trigger` t ON cards.trigger_id = t.trigger_id
 JOIN `magez_cfv_skills` s ON cards.skill_id = s.skill_id

答案 1 :(得分:1)

您应该使用的函数是IIF(),其语法是

IIF([condition],[if true],[if false])