[MySQL光速入门]023 if函数(番外)

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

if语句

IF search_condition THEN
	statement_list
ELSE
	statement_list
END IF;
复制代码

if 函数

IF(expr1,expr2,expr3)
复制代码

含义也很简单: 若是expr1为真, 则执行expr2, 不然执行expr3sql

根据存款, 判断是土豪仍是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;`
复制代码

结果以下:post

image.png

快速跳转

相关文章
相关标签/搜索