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

本文深入讲解MySQL中的条件判断,通过if语句和if函数的实际应用,演示如何根据存款金额判断客户类型,从土豪到low逼,一目了然。文章包含SQL创建表、插入数据及查询操作的完整示例。

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;`
复制代码

结果如下:

快速跳转

转载于:https://juejin.im/post/5cb80eb3e51d456e4514f551

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值