MySQL數(shù)據(jù)庫(kù)查詢(xún)基礎(chǔ),容易查詢(xún),條件查詢(xún),對(duì)查詢(xún)結(jié)果排序
發(fā)表時(shí)間:2023-07-22 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一.SELECT語(yǔ)句 SELECT COL1,COL2,....COLn FROM TABLE1,TABLE2,....TABLEn[WHERE CONDITIONS] -- 查詢(xún)條件[GROUP ...
一.SELECT語(yǔ)句 SELECT COL1,COL2,....COLn FROM TABLE1,TABLE2,....TABLEn
[WHERE CONDITIONS] -- 查詢(xún)條件
[GROUP BY GROUP_BY_LIST] -- 查詢(xún)結(jié)果分組
[HAVING CONDITIONS] -- 查詢(xún)條件-統(tǒng)計(jì)結(jié)果作為條件
[ORDER BY ORDER_LIST[ASC DESC] -- 查詢(xún)結(jié)果排序
二.簡(jiǎn)單查詢(xún)
1.查詢(xún)表的全部行和列
eg:查詢(xún)玩家表中全部的行和列
select user_qq,user_name,user_sex,user_birthday,user_mobile from users;
select * from users;
2.查詢(xún)表的部分列
eg:從玩家表中查詢(xún)玩家QQ和昵稱(chēng)
select user_qq,user_name from users;
3.別名的使用
eg:從玩家表中查詢(xún)玩家QQ和昵稱(chēng),并顯示為‘玩家QQ' 和 '玩家昵稱(chēng)'
select user_qq as '玩家QQ',user_name as '玩家昵稱(chēng)' from users;
select user_qq '玩家QQ',user_name '玩家昵稱(chēng)' from users;
4.DISTINCT關(guān)鍵字 -消除結(jié)果集中的重復(fù)行
eg:顯示參與了游戲的玩家QQ,要求參與了多個(gè)游戲的玩家不重復(fù)顯示QQ
select distinct user_qq from scores;
5.LIMIT關(guān)鍵字 -指定結(jié)果集中數(shù)據(jù)的顯示范圍
eg:顯示玩家表中第3至第5條數(shù)據(jù)
select * from users limit 2,3;
select*from users limit 3 ---只顯示前三條數(shù)據(jù)
三.條件查詢(xún)
1.普通條件查詢(xún)
語(yǔ)法:SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]
eg1:查詢(xún)QQ號(hào)為12301的玩家信息
select * from users where user_qq =12301;
eg2:查詢(xún)分?jǐn)?shù)大于2500分的數(shù)據(jù)
select *from scores where score>2500;
<> -----不等于 >= -----大于等于 <= -----小于等于
eg3:查詢(xún)游戲編號(hào)為1且分?jǐn)?shù)大于4000分的分?jǐn)?shù)信息
select * from scores where gno=1 and score>4000;
邏輯運(yùn)算符:并且 -- and
或者 -- or
非 -- not
eg4: 查詢(xún)游戲編號(hào)為1和2的分?jǐn)?shù)信息
select * from scores where gno=1 or gno=2;
2.模糊查詢(xún)
eg1:查詢(xún)分?jǐn)?shù)在2500(含)到3000(含)的分?jǐn)?shù)
select *from scores where score>=2500 and score<=3000;
select * from scores where score between 2500 and 3000;
eg2:查詢(xún)分?jǐn)?shù)不在2500(含)到3000(含)的分?jǐn)?shù)信息
select * from scores where score not between 2500 and 3000;
eg3:查詢(xún)1987年1月1日到1992年7月31日出生的玩家
select * from users where user_birthday between '1987-01-01' and '1992-0731';
通配符: '_' 一個(gè)字符 Branch like 'L_'
% 任意長(zhǎng)度 Route_Code Like 'AMS-%'
[] 指定范圍內(nèi) Airbusno Like 'AB0[1-5]'
[^] 不在括號(hào)中 Airbusno Like 'AB0[^]'
eg4:查詢(xún)所有姓孫的玩家信息
select * from users where user_name like '孫%';
eg5:查詢(xún)所有非姓孫的玩家信息
select * from users where user_name not like '孫%';
3.查詢(xún)空值得運(yùn)算符
eg:查詢(xún)生日為空的null的玩家信息
select * from users where use_birthday is null;
eg:查詢(xún)生日不為NULL的玩家信息
select * from users where user_birthday is not null;
四 對(duì)查詢(xún)結(jié)果排序
1. 對(duì)指定列進(jìn)行排序(排序依據(jù),排序方式)
語(yǔ)法:SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]
例:查詢(xún)分?jǐn)?shù)表中編號(hào)為1的所有分?jǐn)?shù)信息,并按照分?jǐn)?shù)升序排序
select *from scores where gno=1 order by score asc.
例:查詢(xún)分?jǐn)?shù)表中編號(hào)為1的所有分?jǐn)?shù)信息,并按照分?jǐn)?shù)降序排序
select * from score where gno=1 order by score desc.
2. 對(duì)多列進(jìn)行排序(排序依據(jù),排序方式,優(yōu)先級(jí))
例:查詢(xún)分?jǐn)?shù)表中的所有信息,并按照游戲編號(hào)的升序和分?jǐn)?shù)的降序進(jìn)行排序
select * from scores order by gno asc, score desc
以上就是MySQL數(shù)據(jù)庫(kù)查詢(xún)基礎(chǔ),簡(jiǎn)單查詢(xún),條件查詢(xún),對(duì)查詢(xún)結(jié)果排序的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門(mén)到精通的SQL知識(shí)。