基礎SELECT實例


SELECT查詢語句

  ---進行單條記錄、多條記錄、單表、多表、子查詢……

SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [MAX_STATEMENT_TIME = N] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [PARTITION partition_list] [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]

select書寫使用技巧:

  ①確認需要訪問數據來自哪幾張表

    from來自某張表或者某幾張表

    join添加某張表

    on表連接條件

  記住一點:每關聯一個表就需要加上對應的on條件on條件就是主外鍵條件

  ②通過where條件過濾數據

  ③確認需求里面是否有分組聚合的含義

    分組:group by

    聚合:聚合函數

    聚合條件過濾:having

  ④是否需要排序

    order by

 

1、查詢某張表所有數據

mysql> select * from temp; 解析:*代表所有列,temp代表表名,不帶條件就查詢所有數據

 

2、查詢指定列和條件的數據

mysql> select name,age from temp where age = 22; 解析:查詢name和age這兩列,age 等於22的數據。

 

3、對查詢的數據進行運算操作

mysql> select age+2,age/2,age-2,age*2 from temp where age-2 > 22; mysql> select PLAYRNO,AMOUNT,AMOUNT*6.5 “Ren Min Bi” from PENALTIES; 解析:查詢AMOUNT列數據乘6.5並改列名為Ren Min Bi 使用小括號可以改變運算的優先級

 

4、concat函數,字符串連接

mysql> select NAME,concat(TOWN,STREET,HOUSENO) “player Home Address” from PLAYERS; 解析:利用concat函數將TOWN,STREET,HOUSENO三列的字符串連接起來,別名列為player Home Address(可以在concat里加' '進行分隔) 注意:concat和null進行連接,會導致連接后的數據成為null mysql> select * from t1; +------+--------+--------+
| id   | First  | Last   |
+------+--------+--------+
|    1 | zhang  | jiacai |
|    2 | linghu | NULL   |
+------+--------+--------+ mysql> select id,concat(First,' ',Last) from t1; +------+------------------------+
| id   | concat(First,' ',Last) |
+------+------------------------+
|    1 | zhang jiacai           |
|    2 | NULL                   |
+------+------------------------+

concat_ws函數,指定分隔符的字符串連接

mysql> select id,concat_ws(':',First,Last) "Full Name" from t1; +------+--------------+
| id   | Full Name    |
+------+--------------+
|    1 | zhang:jiacai |
|    2 | linghu       |
+------+--------------+ 圓括號里的第一個位置用來指定字符串連接的分隔符

 

5、as 列別名(或省略,留空格)

mysql> select id as 'num' from t1; mysql> select id 'num',First from t1;

 

6、distinct關鍵字去掉重復數據

mysql> select * from t2; +------+
| num  |
+------+
|    1 |
|    1 |
|    1 |
| 2 | +------+ mysql> select distinct num from t2; +------+ | num | +------+ | 1 | | 2 | +------+ distinct 多列:去重的是同行多列組合的重復數據 mysql> select distinct id, age from temp;

 

7、where條件查詢

where語句中的條件比較:大於>、大於等於>=、小於<、小於等於<=、等於=、不等於<> mysql> select * from tableName where a>2 or a>=3 or a<5 or a<=6 or a=7 or a<>0;

 

8、and 並且、or 或者、not非

mysql> select * from temp where age>20 and name=‘jack’; 解析:查詢name等於jack並且年齡大於20的 mysql> select * from tmep where name=‘jack’ or name=‘jackson’; 解析:查詢name是jack或是jackson的 mysql> select * from temp where not (age > 20); 解析:取小於等於20的數據 mysql> select * from temp where id not in(1, 2); 解析:查詢id數不是1,也不是2的

 

9、between v1 and v2:v1和v2之間

mysql> select * form temp where age between 20 and 25; 解析:查詢age在20和25之間的

 

10、in 查詢:多個條件 類似於or

mysql> select * from temp where id in (1, 2, 3); 解析:查詢id在括號中出現的數據

 

11、like 模糊查詢

%:替代0個或多個字符 _:替代一個字符 mysql> select * from temp where name like ‘j%’; 解析:查詢name以j開頭的(%通配所有) mysql> select * from temp where name like ‘%k%’; 解析:查詢name包含k的 escape轉義 mysql> select * from temp where name like ‘\_%’ escape ‘\’; 解析:指定\為轉義字符,上面的就可以查詢name中以“_”開頭的數據

 

12、is null、is not null

mysql> select * from temp where name is null; 解析:查詢為null的數據 mysql> select * from temp where name is not null; 解析:查詢不為null的數據

 

13、order by排序:desc降序、asc升序

mysql> select * from temp order by id; (默認asc升排序) mysql> select * from temp order by id desc; (指定降序排) 多列組合 mysql> select * from temp order by id, age;

 

14、limit子句:從結果集中選取最前面或最后面的幾行

  通常和order by連用,放其后面

limit  <獲取的行數> [OFFSET <跳過的行數>]

limit [<跳過的行數>,] <獲取的行數> 

mysql> select playerno,name from players order by playerno asc limit 3,5; mysql> select playerno,name from players order by playerno asc limit 5 offset 3; 解析:跳過前面的3行,從第4行開始取,取5行

注意:MySQL5.7 doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

 

15、group by、having 分組聚合

select [聚合函數] 字段名 from 表名

  [where 查詢條件]

  [group by 字段名]

  [having 過濾條件]

mysql> select salary,count(*) from salary_tab -> where salary>=2000
    -> group by salary -> having count(*)>=0; +---------+----------+
| salary  | count(*) |
+---------+----------+
| 2000.00 |        1 |
| 3000.00 |        1 |
+---------+----------+

 

##########欲知詳細解析,請聽下回分解##########


免責聲明!

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



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