請耐心閱讀,下麵有驚喜! 1.創建資料庫 2.創建各表(表結構;約束) 3.添加/插入 數據 4.查詢數據 5.修改數據 6.刪除數據 輔助blog MySQL 獲得當前日期時間 函數 獲得當前日期+時間(date + time)函數:now() mysql> select now(); + +| ...
請耐心閱讀,下麵有驚喜!
1.創建資料庫
CREATE DATABASE QQDB;
2.創建各表(表結構;約束)
/*******************創建********************/ USE QQDB; DROP TABLE IF EXISTS QQUser; CREATE TABLE QQUser ( qqid BIGINT PRIMARY KEY, PASSWORD VARCHAR(20) NOT NULL, lastlogtime DATETIME NOT NULL, online INT NOT NULL, LEVEL INT NOT NULL ); DROP TABLE IF EXISTS baseinfo; CREATE TABLE baseinfo ( qqid BIGINT PRIMARY KEY, nickname VARCHAR(50) NOT NULL, sex INT, age INT NOT NULL, province VARCHAR(50), city VARCHAR(50), address VARCHAR(200), phone CHAR(50), CONSTRAINT `FK_qqid` FOREIGN KEY (`qqid`) REFERENCES `qquser` (`qqid`) ); DROP TABLE IF EXISTS relaion; CREATE TABLE relaion ( qqid BIGINT NOT NULL, relationqqid BIGINT NOT NULL, relationstatus INT NOT NULL, CONSTRAINT `FK_r_qqid` FOREIGN KEY (qqid) REFERENCES `qquser` (`qqid`), CONSTRAINT `FK_r_relationqqid` FOREIGN KEY (relationqqid) REFERENCES `qquser` (`qqid`) );
3.添加/插入 數據
/*******************添加********************/ insert into `qquser`(`QQid`,`Password`,`lastlogTime`,`Online`,`level`) values (1,'a123456','2017-03-02 00:00:00',1,6), (2,'b123456','2017-03-02 00:00:00',2,7), (3,'c123456','2018-03-01 00:00:00',2,6), (4,'d123456','2018-03-02 00:00:00',2,8), (7,'g123456','2012-02-02 00:00:00',0,-1), (8855678,'gues0221','2008-02-21 16:28:20',1,6), (54789625,'add512#&','2008-02-16 17:01:35',2,1), (88662753,'admin0219','2008-02-19 21:08:35',0,5); insert into `baseinfo`(`QQID`,`nickName`,`Sex`,`Age`,`provice`,`City`,`Addres`,`phone`) values (1,'小明',0,18,'山東省','濟南','小村村','004'), (2,'小天',1,15,'山東','聊城','大村村','005'), (3,'小王',1,16,'河南','開封','不知奧','006'), (4,'嘟嘟魚',0,13,'天津','塘沽','呵呵','007'), (8855678,'獨行俠,1,38,'北京','海澱區','解放中路號院123室','003'), (54789625,'蝴蝶飛飛',1,16,'北京','海澱區','亞運村','001'), (88662753,'青青草',0,20,'河南省','安陽','湯陰','002'); INSERT INTO `relaion`(`qqid`,`relationqqid`,`relationstatus`) VALUES(54789625,88662753,0), (88662753,8855678,1), (54789625,8855678,0);
4.查詢數據
##查詢數據 #01.查詢QQ號碼為54789625的所有好友信息,包括QQ號碼,昵稱,年齡 select RelationQQID as QQ號碼,NickName as 昵稱,Age as 年齡 from BaseInfo,Relation where BaseInfo.QQID=Relation.RelationQQID and Relation.QQID=54789625 and RelationStatus=0 或者1 #02.查詢當前線上用戶的信息 SELECT NickName,Province FROM BaseInfo,QQUser WHERE BaseInfo.QQID=QQUser.QQID AND Online=0 #03.查詢北京的、年齡在18至45歲之間的線上用戶的信息 SELECT NickName,Province FROM BaseInfo,QQUser WHERE BaseInfo.QQID=QQUser.QQID AND BaseInfo.Province LIKE '%北京%' AND BaseInfo.Age BETWEEN 18 AND 45 AND Online=0 #04.查詢昵稱為青青草的用戶信息 SELECT NickName,Province,City,Address FROM BaseInfo WHERE NickName='青青草' #05.查詢QQ號碼為54789625的用戶的好友中每個省份的總人數,並且總人數按由大到小排序。 SELECT BaseInfo.Province,COUNT(*) AS 總人數 FROM Relation,BaseInfo WHERE Relation.RelationQQID=BaseInfo.QQID AND Relation.RelationStatus=0 AND Relation.QQID=54789625 GROUP BY BaseInfo.Province ORDER BY 總人數 DESC #06.查詢至少有150天未登錄QQ賬號的用戶信息,包括QQ號碼,最後一次登錄時間、等級、昵稱、年齡,並按時間的降序排列 SELECT QQUser.QQID,QQUser.LastLogTime,QQUser.Level,BaseInfo.NickName,BaseInfo.Age FROM BaseInfo,QQUser WHERE BaseInfo.QQID=QQUser.QQID AND DATEDIFF(NOW(),lastLogTime)>=150 ORDER BY DATEDIFF(NOW(),lastLogTime) DESC #07.查詢QQ號碼為54789625的好友中等級為10級以上的“月亮”級用戶信息。 SELECT NickName,Province FROM QQUser,Relation,BaseInfo WHERE Relation.RelationQQID=BaseInfo.QQID AND Relation.QQID=54789625 AND Relation.RelationStatus=0 AND Relation.RelationQQID=QQUser.QQID AND QQUser.Level>=10 #08.--查詢QQ號碼為54789625的好友中隱身的用戶信息。 SELECT NickName,province FROM Relation INNER JOIN BaseInfo ON Relation.RelationQQID=BaseInfo.QQID AND Relation.QQID=54789625 INNER JOIN QQUser ON QQUser.QQID=RelationQQID AND Online=2 #2代表隱身 AND Relation.RelationStatus=0 #0代表好友 #09.--查詢好友超過20個的用戶信息。 SELECT Nickname,province FROM BaseInfo WHERE QQID IN (SELECT QQID FROM Relation WHERE RelationStatus=0 GROUP BY QQID HAVING COUNT(*)>20 ) #10.為了查看信譽度,管理員需要查詢被當做黑名單人物次數排名前3的用戶 SELECT RelationQQID,COUNT(*) AS 次數 FROM Relation WHERE RelationStatus=1 GROUP BY RelationQQID ORDER BY COUNT(*) DESC LIMIT 3
5.修改數據
##修改數據 #01.假設我的QQ號碼為8855678,今天我隱身登錄 UPDATE QQUser SET Online=2,LastLogTime=NOW() WHERE QQID=8855678 #02.假設我的QQ號碼為8855678,修改我的昵稱為“被淹死的魚”,地址為“解放中路號院123室” UPDATE BaseInfo SET NickName='被淹死的魚',Address='解放中路號院室' WHERE QQID=8855678 #03.假設我的QQ號碼為54789625,將我的好友“青青草”拖進黑名單。 UPDATE Relation SET RelationStatus=1 WHERE QQID=54789625 AND RelationQQID=88662753 #04.為了提高QQ用戶的聊天積極性,把等級小於6級的用戶的等級都提升1個級別。 update QQUser set Level=Level+1 where Level<6 #05.管理員將超過365天沒有登錄過的QQ鎖定(即將等級值設定為-1)。 UPDATE QQUser SET LEVEL=-1 WHERE DATEDIFF(NOW(),lastLogTime)>=365 #06.為了獎勵用戶,將好友數量超過20的用戶等級提升1個級別。 UPDATE QQUser SET LEVEL=LEVEL+1 WHERE QQID IN( SELECT Relation.QQID FROM Relation WHERE RelationStatus=0 GROUP BY Relation.QQID HAVING COUNT(Relation.RelationQQID)>=20) #07.把QQ號碼為54789625的用戶的好友“嘟嘟魚”拖進黑名單中。 UPDATE Relation SET RelationStatus=1 WHERE QQID=54789625 AND RelationQQID= (SELECT QQID FROM BaseInfo WHERE NickName='嘟嘟魚') AND RelationStatus=0
6.刪除數據
##刪除數據 #1.把QQ號碼為54789625的用戶黑名單中的用戶刪除。 DELETE FROM Relation WHERE QQID=54789625 AND RelationStatus=1 #2.QQ號碼為54789625的用戶多次在QQ中發佈違法信息,造成了很壞的影響,因此管理員決定將其刪除。 DELETE FROM Relation WHERE QQID=54789625 OR RelationQQID=54789625 DELETE FROM BaseInfo WHERE QQID=54789625 DELETE FROM QQUser WHERE QQID=54789625 #3.管理員將超過1000天沒有登錄過的QQ刪除。 DELETE FROM Relation WHERE QQID IN ( SELECT QQID FROM QQUser WHERE DATEDIFF(NOW(),LastLogTime)>=1000 ) OR RelationQQID IN ( SELECT QQID FROM QQUser WHERE DATEDIFF(NOW(),LastLogTime)>=1000 ) DELETE FROM BaseInfo WHERE QQID IN ( SELECT QQID FROM QQUser WHERE DATEDIFF(NOW(),LastLogTime)>=1000 ) DELETE FROM QQUser WHERE DATEDIFF(NOW(),LastLogTime)>=1000
輔助blog
MySQL 獲得當前日期時間 函數
獲得當前日期+時間(date + time)函數:now()
mysql> select now();
+---------------------+| now() |+---------------------+| 2008-08-08 22:20:46 |+---------------------+
獲得當前日期+時間(date + time)函數:sysdate()
sysdate() 日期時間函數跟 now() 類似,不同之處在於:now() 在執行開始時值就得到了, sysdate() 在函數執行時動態得到值。看下麵的例子就明白了:
mysql> select now(), sleep(3), now();
+---------------------+----------+---------------------+| now() | sleep(3) | now() |+---------------------+----------+---------------------+| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |+---------------------+----------+---------------------+
sysdate() 日期時間函數,一般情況下很少用到。
MySQL 獲得當前時間戳函數:current_timestamp, current_timestamp()
mysql> select current_timestamp, current_timestamp();
+---------------------+---------------------+| current_timestamp | current_timestamp() |+---------------------+---------------------+| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |+---------------------+---------------------+
MySQL 日期轉換函數、時間轉換函數
MySQL Date/Time to Str(日期/時間轉換為字元串)函數:date_format(date,format), time_format(time,format)
mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
+----------------------------------------------------+| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |+----------------------------------------------------+| 20080808222301 |+----------------------------------------------------+
MySQL 日期、時間轉換函數:date_format(date,format), time_format(time,format) 能夠把一個日期/時間轉換成各種各樣的字元串格式。它是 str_to_date(str,format) 函數的 一個逆轉換。
MySQL Str to Date (字元串轉換為日期)函數:str_to_date(str, format)
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30
可以看到,str_to_date(str,format) 轉換函數,可以把一些雜亂無章的字元串轉換為日期格式。另外,它也可以轉換為時間。“format” 可以參看 MySQL 手冊。
MySQL (日期、天數)轉換函數:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0select to_days('2008-08-08'); -- 733627
MySQL (時間、秒)轉換函數:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605select sec_to_time(3605); -- '01:00:05'
MySQL 拼湊日期、時間函數:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(2001,31); -- '2001-01-31'select makedate(2001,32); -- '2001-02-01'select maketime(12,15,30); -- '12:15:30'
MySQL (Unix 時間戳、日期)轉換函數
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
下麵是示例:
select unix_timestamp(); -- 1218290027select unix_timestamp('2008-08-08'); -- 1218124800select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select from_unixtime(1218290027); -- '2008-08-09 21:53:47'select from_unixtime(1218124800); -- '2008-08-08 00:00:00'select from_unixtime(1218169800); -- '2008-08-08 12:30:00'
select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
MySQL 日期時間計算函數
MySQL 為日期增加一個時間間隔:date_add()
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 dayselect date_add(@dt, interval 1 hour); -- add 1 hourselect date_add(@dt, interval 1 minute); -- ...select date_add(@dt, interval 1 second);select date_add(@dt, interval 1 microsecond);select date_add(@dt, interval 1 week);select date_add(@dt, interval 1 month);select date_add(@dt, interval 1 quarter);select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day
MySQL adddate(), addtime()函數,可以用 date_add() 來替代。下麵是 date_add() 實現 addtime() 功能示例:
mysql> set @dt = '2008-08-09 12:12:33';
mysql>
mysql> select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+| date_add(@dt, interval '01:15:30' hour_second) |+------------------------------------------------+| 2008-08-09 13:28:03 |+------------------------------------------------+
mysql> select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+| date_add(@dt, interval '1 01:15:30' day_second) |+-------------------------------------------------+| 2008-08-10 13:28:03 |+-------------------------------------------------+
MySQL 為日期減去一個時間間隔:date_sub()
mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
+----------------------------------------------------------------+| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |+----------------------------------------------------------------+| 1997-12-30 22:58:59 |+----------------------------------------------------------------+
MySQL date_sub() 日期時間函數 和 date_add() 用法一致,不再贅述。
MySQL 日期、時間相減函數:datediff(date1,date2), timediff(time1,time2)
MySQL datediff(date1,date2):兩個日期相減 date1 - date2,返回天數。select datediff('2008-08-08', '2008-08-01'); -- 7select datediff('2008-08-01', '2008-08-08'); -- -7
MySQL timediff(time1,time2):兩個日期相減 time1 - time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08select timediff('08:08:08', '00:00:00'); -- 08:08:08
註意:timediff(time1,time2) 函數的兩個參數類型必須相同。
MySQL 時間戳(timestamp)轉換、增、減函數:
timestamp(date) -- date to timestamptimestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --timestampdiff(unit,datetime_expr1,datetime_expr2) --
請看示例部分:
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
MySQL timestampadd() 函數類似於 date_add()。select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12
select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7
MySQL timestampdiff() 函數就比 datediff() 功能強多了,datediff() 只能計算兩個日期(date)之間相差的天數。
MySQL 時區(timezone)轉換函數
convert_tz(dt,from_tz,to_tz)
select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00
時區轉換也可以通過 date_add, date_sub, timestampadd 來實現。
select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00