1、mysql查询今天、昨天、上周
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
#查询本季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
select *
from user
where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())
select *
from [ user ]
where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now())
select *
from [ user ]
where pudate between 上月最后一天
and 下月第一天
where date(regdate) = curdate();
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30
2 搜索选修课程是税收基础的学生信息
SELECTs.*
FROMc
JOINscONc.id=sc.cid
JOINsONs.id=sc.sid
WHEREc.cn='税收基础'
3 查询选修课程大于2门课的学生信息
SELECTm.*
FROM(
SELECTs.*,count(*)ASaaa
FROMs
JOINscONs.id=sc.sid
)m
或者
SELECTs.*,count(*)
FROMs
JOINscONs.id=sc.sid
HAVINGcount(*)>3
4 查询学员朱欣磊选修的课程信息
SELECTs.*
FROMs
JOINscONs.id=sc.sid
WHEREs.sn='朱欣磊'
5 查询没有选择数学课的学生信息
SELECT*
FROMs
WHEREidNOT
IN(
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
WHEREc.cn='数学'
)
先查找的选修了数学的学生,然后排除的
6 查询没门课选修的人数
SELECTcn,count(*)
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
GROUPBY cn
7 查询每个学员选修了几门课程
SELECTsn,count(*)
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
GROUPBYsn
8 查询选修课程不及格的学生信息及课程信息
SELECT*
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
WHEREsc.g<60
9查询各门课的平均成绩,输出课程名及平均成绩,最高,最低
SELECTc.cn,avg(g),max(g),min(g)
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
GROUPBYcn
10 查询至少有两人选修的课程
SELECTcn
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
GROUPBYcn
HAVINGcount(*)>2
11查询税收基础成绩不低于平均成绩的学生信息及其成绩
SELECTsn,sc.g
FROMs
JOINscONs.id=sc.sid
JOINcONc.id=sc.cid
GROUPBYcn
HAVINGsc.g>avg(sc.g)
这样写是不正确的,这里
SELECT *
FROM s
JOIN sc ON s.id=sc.sid
JOIN c ON c.id=sc.cid
where c.cn = '税收基础' and g > (select avg(g) from sc where cid = 1)
12 查询年龄是21岁的平均成绩最高的学生信息
select max(abc.aaa) from (select sa,avg(g) as aaa from s join sc on World's shortest URL shortener = sc.sid join c on c.id = sc.cid group by World's shortest URL shortener) as abc where Automated Buildings Company = 21
13 查询选修过课的学生的总人数
select *,count(*) from sc group by sc.sid
这是不行的,这种写法以为是以学生来分类的,所以取出的是每个学生选修了几门课
select count(*) from (select *,count(*) from sc group by sc.sid)