SQL Cookbook:检索记录

1、where子句中,and优先级比or高

1 select * from film where film_id < 10 or title like '%ARK' and length = 50\G

等同于以下查询:

1 select * from film where film_id < 10 or (title like '%ARK' and length = 50)\G

 

2、连接列值:concat

1 select concat('title: ', title, ' description: ', description) as info from film\G

 

3、使用case语句

1 select title, 
2 case 
3     when length <= 70 then 'short' 
4     when length > 70 then 'long' 
5 end 
6 as info from film where film_id = 1\G

 

4、随机选择n条数据

1 select film_id, title from film order by rand() limit 5\G

 

5、查找空值

1 select film_id, title from film where title is null\G

null要使用is

 

6、为空值设定默认值

1 select film_id, coalesce(title, '') as title from film\G

 

转载于:https://www.cnblogs.com/zcy-backend/p/6805529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值