MySQL 關於年齡 生日的7道查詢題目

2021-09-02 14:08:26 字數 2078 閱讀 4457

create table student(

s_id varchar(20),

s_name varchar(20) not null default'',

s_birth varchar(20) not null default'',

s_*** varchar(10) not null default'',

primary key(s_id)

);

insert into student values('01' , '趙東' , '1990-01-01' , '男');

insert into student values('02' , '錢明' , '1990-12-21' , '男');

insert into student values('03' , '孫念' , '1990-05-20' , '男');

insert into student values('04' , '李勒' , '1990-08-06' , '男');

insert into student values('05' , '周光' , '1991-12-01' , '女');

insert into student values('06' , '王蘭' , '1992-03-01' , '女');

insert into student values('07' , '孫良' , '1989-07-01' , '女');

insert into student values('08' , '金星' , '1990-01-20' , '女');

查詢各學生的年齡

select *,year(now())-year(s_birth) age from student;
查詢本週過生日的學生

select s_id from student where date_format(s_birth,'%m%d') between date_format('2018-12-03', '%m%d') and date_format('2018-12-09', '%m%d');
查詢下週過生日的學生

select s_id from student where date_format(s_birth,'%m%d') between date_format('2018-12-10', '%m%d') and date_format('2018-12-16', '%m%d');
查詢本月過生日的學生

select * from student where month(s_birth)=month(now());
或者

select s_id from student where date_format(s_birth,'%m')=date_format(now(),'%m');
查詢下月過生日的學生

select * from student where month(s_birth)=month(now())+1;
或者

select s_id from student where date_format(s_birth,'%m')=date_format(now(),'%m')+1;
查詢最近30天內過生日的同學

select * from student  where dayofyear(now())  - dayofyear(s_birth) between 0 and 30;
查詢當天過生日的學生

select * from student where month(s_birth)=month(now()) and day(s_birth)=day(now());
或者

select s_id from student where date_format(s_birth,'%m%d')=date_format(now(),'%m%d');

mysql關於日期 MySQL關於日期的查詢sql

查詢今天的記錄 select from wmacc.cash trade record where to days create date to days now select from wmacc.cash trade record where date format create date,y ...

MySQL根據出生日期計算年齡的五種方法比較

select date format from days to days now to days birthday y 0 as age方法一,作者也說出了缺陷,就是當日期為未來日期時結果為0,而不是負數 這裡使用了5個函式和兩個運算子。select date format now y date f...

MySQL 根據出生日期計算年齡的五種方法比較

select date format from days to days now to days birthday y 0as age方法一,作者也說出了缺陷,就是當日期為未來日期時結果為0,而不是負數 這裡使用了5個函式和兩個運算子。select date format now y date fo...