mysql的一些小知识点

文章介绍了SQL查询中的高级技巧,包括日期范围筛选、平均确认率计算、NULL值处理以及使用COALESCE、IFNULL和CASE等表达式进行数据清洗和筛选,同时展示了如何利用模运算符判断奇偶性进行数据库查询

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、select a.id from Weather a,Weather b where

a.recordDate=DATE_ADD(b.recordDate,INTERVAL 1 DAY) and a.Temperature > b.Temperature

注:DATE_ADD(b.recordDate,INTERVAL 1 DAY) 前一天

2、select s.user_id,round(avg(if(c.action = 'confirmed',1,0)),2) confirmation_rate

from Signups s left join Confirmations c on s.user_id=c.user_id group by s.user_id;

注:round(avg(if(c.action = 'confirmed',1,0)),2)     

确认率(confirmed占所有action的比率)四舍五入保留两位小数

3、(1)SELECT COALESCE(column_name, 0) FROM table_name;

(2)SELECT IFNULL(column_name, 0) FROM table_name;

(3)SELECT 
            CASE 
                WHEN column_name IS NULL THEN 0 
                ELSE column_name 
            END

        FROM table_name;

注:将null值替换成0,(2)不适合多表或group by 

4、(1)select * from cinema where mod(id,2) = 1 and description !="boring" order by rating desc

(2)select * from employees where emp_no % 2 = 1 and last_name <> 'Mary'  order by hire_date desc;

(3)select * from employees where emp_no & 1 = 1 and last_name <> 'Mary' order by hire_date desc;
注:(1)mod(a,b) 在sql中的意思是 a / b 的余数;mod(id, 2)=1 是指id是奇数。mod(id, 2)=0 是指id是偶数。

(2)id是奇数:(id % 2) = 1; id是偶数:(id % 2) = 0;

(3)id是奇数:(id & 1) = 1; id是偶数:(id & 1) = 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值