第23课 [番外] if函数

2019-04-10  本文已影响0人  猫哥的技术博客

if语句可以进行条件判断, 实际上if函数也可以

if语句

IF search_condition THEN
    statement_list
ELSE
    statement_list
END IF;

if 函数

IF(expr1,expr2,expr3)

含义也很简单: 如果expr1为真, 则执行expr2, 否则执行expr3

根据存款, 判断是土豪还是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;`

结果如下:

image.png
上一篇 下一篇

猜你喜欢

热点阅读