if语句可以进行条件判断, 实际上if函数也可以
if语句
IF search_condition THEN statement_listELSE statement_listEND 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;`复制代码
结果如下: