QQ数据库管理
1)查詢數據
#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
2)修改數據
#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
3)刪除數據
#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
總結
- 上一篇: ADAMoracle预言机的发展趋势和特
- 下一篇: RPG游戏