標籤:
要從資料庫中查詢,要使用SQL的select語句,標準select查詢有select子句,where子句,order by子句組成。
查詢資料的標準結構為:select 列名 from 表名 where 條件 order by 列名 asc(升序)/desc(降序)
查詢操作的分類:1、投影操作,指定查詢結果中能顯示哪些列
2、選擇操作,指定哪行出現在結果中
3、排序操作,指定查詢的結果以什麼樣的順序顯示
投影操作:select 列1,列2 from 表名
表首碼:select 表名.列名 from 表名
列別名(才顯示結果的時候更改列名):select 列 as 別名 from 表名
計算資料行:select 列名 計算符 計算量
排除重複資料:select distinct 列名 from 表名 (多個列時,使用組合匹配,只有多個列相同時才會排除)
表中列拼接:select concat(列1,列2,列3) from 表名
返回限定數的查詢:select 列1 ,列2 from 表名 limit 開始序號,返回的行數
注意:limit 序號是從0開始,limit只能用於mySQL當中
查詢表中符合約束條件的個數:select count(列名)from 表名 where 約束條件
查詢表中條件1至條件2的資料:select 列名 from 表名 where 列名 between 條件1 and 條件2
查詢合格資料:select 列名 from 表名 where 列名 in(約束條件)
查詢排除條件的資料:select 列名 from 表名 where 列名 out(約束條件)
模糊查詢:
查詢列A,列B當列C有兩個字元的資料:select 列A,列B from 表名 where 列C like‘_ _‘
查詢表中列C含有’張‘字元的資料:select * from 表名 where 列名C like ‘張%‘
轉譯a後的內容:select * from 表名 where 列名 like ‘%a%%‘ escape ‘a‘
查詢表中所有列名為空白的資料:select * from 表名 where 列名 is null
查詢表中所有列名不為空白的資料:select * from 表名 where 列名 is not null
基本的查詢SQL的執行順序:1、執行from 2、where 條件 3.select 投影列 4.執行Oder by 排序
2016.3.25(mySQL簡單的資料查詢)