第23課 [番外] if函數(shù)

if語句可以進行條件判斷, 實際上if函數(shù)也可以

if語句

IF search_condition THEN
    statement_list
ELSE
    statement_list
END IF;

if 函數(shù)

IF(expr1,expr2,expr3)

含義也很簡單: 如果expr1為真, 則執(zhí)行expr2, 否則執(zhí)行expr3

根據(jù)存款, 判斷是土豪還是low逼

drop table if exists test;

create table `test` (
  `id` int(11) not null auto_increment,
  `name` varchar(10) not null,
  `account` int(11) not null,
  `age` tinyint(1) not null,
  `sex` char(1) not null default '男',
  primary key (`id`)
) engine=innodb auto_increment=1 default charset=utf8;

insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (1, '張三', 3000, 18, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (2, '李四', 4000, 28, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (3, '王五', 5000, 38, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (4, '趙六', 6000, 48, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (5, '孫七', 2000, 19, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (6, '周八', 1000, 29, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (7, '吳老九', 9000, 39, '男');
insert into `test`(`id`, `name`, `account`, `age`, `sex`) 
    values (8, '馮老十', 8000, 49, '男');
select id,
    name,
    account,
    age,
    sex,
    if(account >= 5000,'土豪','low逼') as type 
from test;`

結(jié)果如下:

image.png
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容