基礎SELECT實例

SELECT查詢語句mysql

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

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書寫使用技巧:函數

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

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

    join添加某張表orm

    on錶鏈接條件blog

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

  ②經過where條件過濾數據字符串

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

    分組:group by

    聚合:聚合函數

    聚合條件過濾:having

  ④是否須要排序

    order by

 

一、查詢某張表全部數據

mysql> select * from temp; 解析:*表明全部列,temp表明表名,不帶條件就查詢全部數據

 

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

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

 

三、對查詢的數據進行運算操做

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 使用小括號能夠改變運算的優先級

 

四、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       |
+------+--------------+ 圓括號裏的第一個位置用來指定字符串鏈接的分隔符

 

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

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

 

六、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;

 

七、where條件查詢

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

 

八、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的

 

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

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

 

十、in 查詢:多個條件 相似於or

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

 

十一、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中以「_」開頭的數據

 

十二、is null、is not null

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

 

1三、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;

 

1四、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'

 

1五、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 |
+---------+----------+

 

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

相關文章
相關標籤/搜索