一.select語句 select col1,col2,….coln from table1,table2,….tablen
[WHERE CONDITIONS] — 查詢條件
[GROUP BY GROUP_BY_LIST] — 查詢結(jié)果分組
[HAVING CONDITIONS] — 查詢條件-統(tǒng)計結(jié)果作為條件
[ORDER BY ORDER_LIST[ASC|DESC] — 查詢結(jié)果排序
?
二.簡單查詢
1.查詢表的全部行和列
eg:查詢玩家表中全部的行和列
?select? user_qq,user_name,user_sex,user_birthday,user_mobile from users;
?select * from users;
2.查詢表的部分列
eg:從玩家表中查詢玩家QQ和昵稱
select user_qq,user_name from users;
3.別名的使用
eg:從玩家表中查詢玩家QQ和昵稱,并顯示為‘玩家QQ’ 和 ‘玩家昵稱’
select user_qq as ‘玩家QQ’,user_name as ‘玩家昵稱’ from users;
select user_qq ‘玩家QQ’,user_name ‘玩家昵稱’ from users;
4.DISTINCT關(guān)鍵字 -消除結(jié)果集中的重復(fù)行
eg:顯示參與了游戲的玩家QQ,要求參與了多個游戲的玩家不重復(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ù)
?
三.條件查詢
1.普通條件查詢
語法:SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]
eg1:查詢QQ號為12301的玩家信息
select * from users where user_qq =12301;
eg2:查詢分數(shù)大于2500分的數(shù)據(jù)
select *from scores where score>2500;
—–不等于??? >= —–大于等于???
eg3:查詢游戲編號為1且分數(shù)大于4000分的分數(shù)信息
select * from scores where gno=1 and score>4000;
邏輯運算符:并且 — and
???????????????? 或者 — or
???????????????? 非?? — not
eg4: 查詢游戲編號為1和2的分數(shù)信息
select * from scores where gno=1 or gno=2;
2.模糊查詢
eg1:查詢分數(shù)在2500(含)到3000(含)的分數(shù)
select *from scores where score>=2500 and score
select * from scores where score between 2500 and 3000;
eg2:查詢分數(shù)不在2500(含)到3000(含)的分數(shù)信息
select * from scores where score not between 2500 and 3000;
eg3:查詢1987年1月1日到1992年7月31日出生的玩家
select * from users where user_birthday between ‘1987-01-01’ and ‘1992-0731’;
通配符: ‘_’??? 一個字符???? Branch like ‘L_’
??????????? %???? 任意長度?????Route_Code Like ‘AMS-%’
??????????? []?????指定范圍內(nèi)???Airbusno Like ‘AB0[1-5]’
??????????? [^]???不在括號中?? Airbusno Like ‘AB0[^]’
eg4:查詢所有姓孫的玩家信息
select * from users where user_name like ‘孫%’;
eg5:查詢所有非姓孫的玩家信息
select * from users where user_name not like ‘孫%’;
3.查詢空值得運算符
eg:查詢生日為空的null的玩家信息
select * from users where use_birthday is null;
eg:查詢生日不為NULL的玩家信息
select * from users where user_birthday is not null;
?
四 對查詢結(jié)果排序
1. 對指定列進行排序(排序依據(jù),排序方式)
語法:SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]
例:查詢分數(shù)表中編號為1的所有分數(shù)信息,并按照分數(shù)升序排序
select *from scores where gno=1 order by score asc.
例:查詢分數(shù)表中編號為1的所有分數(shù)信息,并按照分數(shù)降序排序
select * from score where gno=1 order by score desc.
2. 對多列進行排序(排序依據(jù),排序方式,優(yōu)先級)
例:查詢分數(shù)表中的所有信息,并按照游戲編號的升序和分數(shù)的降序進行排序
select * from scores order by gno asc, score desc