資料庫日期操作
1. 查詢各學生的年齡,只按年份來算
-- mysql 寫法一:
select sid,sname,
year
(now()
)-year
(sage) 年齡 from student
-- mysql 寫法二:
select sid,sname,date_format(
now(),
'%y'
)-date_format(sage,
'%y'
) 年齡 from student
-- mysql 寫法三:
select sname,
left
(now()
,4)-
left
(sage,
4) 年齡 from student
2. 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
-- mysql寫法三
select sname, timestampdiff(
year
, sage,
now())
as age from student
3. 查詢本週過生日的學生
select
*from
(select
*,date_sub(curdate(),
interval weekday(curdate())
day) firstdayofweek
, concat_ws(
'-',
year
(curdate())
,date_format(sage,
'%m-%d'
)) thisyear
,concat_ws(
'-',
year
(curdate())
+1,date_format(sage,
'%m-%d'
)) nextyear
,date_add(curdate(),
interval
6-weekday(curdate())
day) lastdayofweek
from student
) t1
where
thisyear>= firstdayofweek and thisyear<=lastdayofweek
ornextyear>= firstdayofweek and nextyear<=lastdayofweek
4. 查詢下週過生日的學生
select
*from
(select
*,date_add(curdate(),
interval
7-weekday(curdate())
day) firstdayofnextweek
, concat_ws(
'-',
year
(curdate())
,date_format(sage,
'%m-%d'
)) thisyearbirthday
,concat_ws(
'-',
year
(curdate())
+1,date_format(sage,
'%m-%d'
)) nextyearbirthday
,date_add(curdate(),
interval
13-weekday(curdate())
day) lastdayofnextweek
from student
) t1
where
thisyearbirthday>= firstdayofnextweek and thisyearbirthday<=lastdayofnextweek
ornextyearbirthday>= firstdayofnextweek and nextyearbirthday<=lastdayofnextweek
5.查詢本月過生日的學生
-- mysql寫法一:
select sid,sname,sage,date_format(sage,
'%m'
),date_format(
now(),
'%m'
)from student where date_format(sage,
'%m'
)=date_format(
now(),
'%m'
)-- mysql寫法二:
select sid,sname,sage,
month
(now()
),month
(sage) 月份 from student where
month
(now()
)=month
(sage)
-- mysql寫法三:
select
*from student where substr(sage,6,
2)=substr(curdate(),
6,2)
6. 查詢下月過生日的學生
注意:要考慮下個月跨年的情況
select s.sid,s.sname,s.sage,t1.nextmonth
from student s,
(select
case
when date_format(
now(),
'%m'
)<
12then date_format(
now(),
'%m')+
1else
1end nextmonth
) t1
where date_format(s.sage,
'%m'
)=t1.nextmonth
7… 查詢未來7天內(包含今天)過生日的學生
select
*from
(select
*,curdate(
) today
,concat_ws(
'-',
year
(curdate())
,date_format(sage,
'%m-%d'
)) thisyearbirthday
,concat_ws(
'-',
year
(curdate())
+1,date_format(sage,
'%m-%d'
)) nextyearbirthday
,date_add(curdate(),
interval
6day
) lastday
from student
) t1
where
thisyearbirthday>=today and thisyearbirthday<=lastday
ornextyearbirthday>= today and nextyearbirthday<=lastday
8.查詢未來30天(包含今天)即將過生日的學生
select
*from
(select
*,curdate(
) today
,concat_ws(
'-',
year
(curdate())
,date_format(sage,
'%m-%d'
)) thisyearbirthday
,concat_ws(
'-',
year
(curdate())
+1,date_format(sage,
'%m-%d'
)) nextyearbirthday
,date_add(curdate(),
interval
29day
) lastday
from student
) t1
where
thisyearbirthday>=today and thisyearbirthday<=lastday
ornextyearbirthday>= today and nextyearbirthday<=lastday
9.查詢今天過生日的學生
select sid,sname,sage,
month
(sage) 月,
day(sage) 日 from student where
month
(now()
)=month
(sage)
andday
(now()
)=day(sage)
資料庫常見操作
一 oracle使用步驟 1 建立表空間 oracle的表是放在表空間中的,他沒有資料庫的概念 以system manager的身份登入plsql create tablespace datafile c oracle product data dbf size 10m autoextend on ...
資料庫的不常見操作
建立 public void createtable dbtable table else if nindex table.fields.count strsql nindex strsql executenonquery strsql 包含架構資訊的 system.data.datatable 獲...
MySQL資料庫常見操作
1.為所有使用者授權 grant all privileges on to 使用者名稱 identified by 密碼 2.資料庫基礎操作 1 登陸mysql資料庫 mysql u root p,回車後再輸入密碼即可 2 檢視所有資料庫 show databases 3 刪除某個資料庫 drop ...