如何在MySQL中实现三元条件运算符?
三元条件运算符看起来像?:在C,C++,Java等编程语言中。语法如下-
(yourCondition) ? statement1:statement2;
在上述语法中,如果yourCondition为true,则statement1将评估;如果yourCondition为false,则statement2将评估。
但是以上语法在MySQL中不起作用。我们可以将IF()MySQL的功能用于相同的目的。
让我们看一个例子-
情况1
mysql> select if(3 > 5,'Condition is true','Condition is not true') as ConditionalResult;
以下是第二条语句求值的输出,因为3不大于5-
+-----------------------+ | ConditionalResult | +-----------------------+ | Condition is not true | +-----------------------+ 1 row in set (0.00 sec)
情况二
mysql> select if(3 < 5,'Condition is true','Condition is not true') as ConditionalResult;
以下是第一条语句求值的输出,因为3小于5-
+-------------------+ | ConditionalResult | +-------------------+ | Condition is true | +-------------------+ 1 row in set (0.00 sec)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短