首頁  >  文章  >  資料庫  >  MySQL資料庫查詢基礎,簡單查詢,條件查詢,對查詢結果排序

MySQL資料庫查詢基礎,簡單查詢,條件查詢,對查詢結果排序

大家讲道理
大家讲道理原創
2017-08-19 10:48:381692瀏覽

一.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 *

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;

#邏輯運算   -- 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<=3000;

select * from scores where score<=3000;

select * from scores where score<=3000;

select * from scores where score<=3000;

select * from scores where score<=3000;

select * from scores 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_'

            %       []     指定範圍內   Airbusno Like 'AB0[1-5 ]'

            [^]   在括號中   Airbusno Like 'AB0[^]'

eg4:查詢所有姓中的玩家資料_user from name#select '孫%';

eg5:查詢所有非姓孫的玩家資訊

select * 從 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###

以上是MySQL資料庫查詢基礎,簡單查詢,條件查詢,對查詢結果排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn