sql 練習題 41 45 時間的用法

2022-09-05 07:42:08 字數 1508 閱讀 8758

題目41 :

按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一(就是根據出生日期算,每個人的年齡)

分析:要引入時間差函式timestampdiff  ,

時間差函式timestampdiff 的用法:

timestampdiff函式,有引數設定,可以精確到天(day)、小時(hour),分鐘(minute)和秒(second),使用起來比datediff函式更加靈活。對於比較的兩個時間,時間小的放在前面,時間大的放在後面。

select timestampdiff(day, 時間a ,時間b);

意思是比較: 時間a 與時間b相差的天數

那麼針對這一題目,我們要用year:

select student.id as 學生編號,student.stdentname as 學生姓名,

timestampdiff(year,student.age,curdate()) as 學生年齡

題目42:查詢本週過生日的學生

select *

from student

whereweekofyear(student.age)=weekofyear(curdate());

weekofyear(date)

返回日期的日曆星期數作為範圍從1到53的數字。weekofyear()是乙個相容性函式,等同於week(日期,3)。

43:查詢下週過生日的學生
select *

from student

where weekofyear(student.sage)=weekofyear(curdate())+1;

44:查詢本月過生日的學生

select month(createtime) from life_unite_product   --取時間欄位的月值

45:查詢下月過生日的學生

分析:這題與上一題目是一樣的,只不過預期時間晚一月

select *

from student

where month(student.sage)=month(curdate())+1;

這一期都是跟時間有關的,時間相關的函式還有很多沒有用到,先寫這麼多吧,後面遇到了再寫

完成StopWatch程式設計練習題時遇到的一些問題

1 public string printlist 5 1 random list new random count random型別不可轉換為int 關於 無法從靜態上下文中引用非靜態 變數 this class randomnumbersequence 放入 public class stopw...

很多注釋的 習題9 1 時間換算

define crt secure no warnings 這行不用管,要不要無所謂,我不加這行scanf會報錯 include include struct time int main if total 3600 1 else int min total 3600 同上,這裡是分鐘 if min ...

乙個關於SQL語句的練習題目

有這樣乙個表。id name age 1 to 12 2 thow 25 3 rwo 23 4 soa 14 5 flo 20 6 tie 20 7 fm 18 8 soa 20 9 flo 16 10 tie 26 11 soa 23 12 rwo 17 問題1 按名字重排,若名字有重複的,則取年...