MySQL數據庫6 -查詢基礎,簡單查詢,條件查詢,對查詢結果排序


一.SELECT語句 SELECT COL1,COL2,....COLn FROM TABLE1,TABLE2,....TABLEn

[WHERE CONDITIONS] -- 查詢條件

[GROUP BY GROUP_BY_LIST] -- 查詢結果分組

[HAVING CONDITIONS] -- 查詢條件-統計結果作為條件

[ORDER BY ORDER_LIST[ASC|DESC] -- 查詢結果排序

 

二.簡單查詢

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關鍵字 -消除結果集中的重復行

eg:顯示參與了游戲的玩家QQ,要求參與了多個游戲的玩家不重復顯示QQ

select distinct user_qq from scores;

5.LIMIT關鍵字 -指定結果集中數據的顯示范圍

eg:顯示玩家表中第3至第5條數據

select * from users limit 2,3;

select*from users limit 3 ---只顯示前三條數據

 

三.條件查詢

1.普通條件查詢

語法:SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]

eg1:查詢QQ號為12301的玩家信息

select * from users where user_qq =12301;

eg2:查詢分數大於2500分的數據

select *from scores where score>2500;

<> -----不等於    >= -----大於等於    <=  -----小於等於

eg3:查詢游戲編號為1且分數大於4000分的分數信息

select * from scores where gno=1 and score>4000;

邏輯運算符:並且 -- and

                 或者 -- or

                 非   -- not

eg4: 查詢游戲編號為1和2的分數信息

select * from scores where gno=1 or gno=2;

2.模糊查詢

eg1:查詢分數在2500(含)到3000(含)的分數

select *from scores where score>=2500 and score<=3000;

select * from scores where score between 2500 and 3000;

eg2:查詢分數不在2500(含)到3000(含)的分數信息

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-%'

            []     指定范圍內   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;

 

四 對查詢結果排序

1. 對指定列進行排序(排序依據,排序方式)

語法:SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]

例:查詢分數表中編號為1的所有分數信息,並按照分數升序排序

select *from scores where gno=1 order by score asc.

例:查詢分數表中編號為1的所有分數信息,並按照分數降序排序

select * from score where gno=1 order by score desc.

2. 對多列進行排序(排序依據,排序方式,優先級)

例:查詢分數表中的所有信息,並按照游戲編號的升序和分數的降序進行排序

select * from scores order by gno asc, score desc

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM