查找今天过生日的同学
这里表中已经存储了生日,所以思路是取出date_birth去今天的日期相比较
SELECT NAME,date_birth FROM org_mhi_studentfiles WHERE MID(date_birth,6,5) = MID(CURDATE(),6,5);
-- or
SELECT NAME,date_birth FROM org_mhi_studentfiles WHERE MID(date_birth,6,5) = DATE_FORMAT(NOW(),'%m-%d');
最年轻的同学
SELECT NAME,date_birth FROM org_mhi_studentfiles WHERE date_birth = (SELECT MAX(date_birth) FROM org_mhi_studentfiles);
-- or
SELECT NAME,date_birth FROM org_mhi_studentfiles ORDER BY date_birth DESC LIMIT 0,1;
- limit 语法
limit 开始行,行数
limit 行数
- limit 和offset
-- 从第一行开始,取三行
limit 0,3
limit 3 offset 0
-- 例如:
SELECT NAME,date_birth FROM org_mhi_studentfiles ORDER BY date_birth DESC LIMIT 0,3;
SELECT NAME,date_birth FROM org_mhi_studentfiles ORDER BY date_birth DESC LIMIT 3 OFFSET 0;
结果都是:
生日倒数排名第三
这里有一个重复值
SELECT NAME,date_birth FROM org_mhi_studentfiles WHERE date_birth =
(SELECT DISTINCT date_birth FROM org_mhi_studentfiles ORDER BY date_birth DESC LIMIT 2,1);
结果: