MySQL查询数据详细
查詢所有字段
?????? Select * from 表名;?? ?//from就是從…開始
查詢指定字段
?????? Select 字段名 from 表名;
查詢多個字段
?????? Select 字段1,字段2,字段3…? from 表名;
查詢指定記錄
?????? Select 字段名 from 表名 where 查詢條件;? //where在哪里
操作符 說明
?=????? 相等
<>,!=??? 不相等
?<????? 小于
<=???? 小于等于
>?????? 大于
>=????? 大于等于
?BETWEEN 位于兩端之間
帶in關鍵字的查詢
?????? Select 字段名 from 表名 where 列名 in 條件? //in操作符來查詢滿足指定范圍內的條件,條件之間用逗號分開,滿足一個條件即可
帶between and 的范圍查詢
?????? Select 字段名 from 表名 where 字段名 between 條件 and 條件;between and用來查詢某個范圍內的值,需要兩個參數,開始值和結束值,如果字段值滿足則返回
帶like的字符匹配查詢
?????? Select 字段名 from 表名 where 字段名 like ‘b%’ //like是查找相似值,%是匹配任意長度的字符,甚至包括零字符? _是指匹配任意一個字符
查詢空值
?????? Select 字符名 from 表名 where 字符名 is null;
?????? Select 字符名 from 表名 where isnull(字符名);
?????? Select 字符名 from 表名 where 字符名 < = > null;
帶and的多條件查詢
?????? Select 字符名 from 表名 where 條件 and 條件 ;? //and操作符只有滿足所有查詢條件的記錄才會被返回,可以使用兩個甚至多個
帶or的多條件查詢
?????? Select 字符名 from 表名 where 條件 or 條件 //只需要滿足一個條件即可,可以多個條件
查詢結果不重復
?????? Select distinct字符名 from 表名 ; //distinct不同的有區別的
對查詢結果排序
?????? select 字符名 from 表名? order by 字符名 ; //order by以…排序 desc 是從降序? 默認是升序asc
分組查詢
?????? 查詢組里有幾個
?????? Select 列名1 count(*) from 表名 group by 列名1; //count計算? group by分組
查詢最大量
?????? Select 列名1 max(列名2) from 表名 group by 列名1; //max最大值
查詢最小值
Select 列名1 min(列名2) from 表名 group by 列名1; //min最小值
總計
? Select 列名1 sum(列名2) from 表名 group by 列名1; //sum總計
平均值
Select 列名1 avg(列名2) from 表名 group by 列名1; //avg 平均值
根據列名1對表中的數據進行分組,將列名2中數據顯示出來
?????? Select 列名1,group_concat(列名2) from 表名 group by 列名1;//group__concat顯示出(列)中的數據
使用having過濾分組根據列1對表中的數據進行分組,并顯示列2大于值的分組信息
select 列1,group_concat(列2) from 表名group by? 列1 having count(列2)>值;//having是擁有 count計數
在group by 中使用with rollup 使用 with rollup 之后增加一條記錄,該記錄計算查詢出的所有記錄的總和,及統計記錄數量
?select 列名1,count(*) from 表名 group by 列名1 with rollup; //with rollup及統計記錄數量
多字段分組
?????? Select 列1 from 表名 group by 列1,列2; // Group by 關鍵字通常和集合函數一起使用,例如:MAX()、MIN()、COUNT()、SUM()、AVG()。
查詢價格大于100的訂單號和總訂單價格
?????? Select 列1,sum(價格*個數) from 表名 group by 列1 having? sum (價格*個數) >100; ?//having比較 sum是總計
使用limit限制查詢結果和數量
?????? Select 列名 from 表名 limit 查看幾行; ?//可以4,3 意思就是從第四行開始數三個
聚合函數查詢
?
演示函數count的意思
?????? Select count(列) from 表名 //count 返回列的行數(計數) 不計空值
可以計算組里的行數
?????? Select 列1,count(列2) from 表名 group by 列1; //統計組里面列2的個數
演示sum函數的意思? 注意:sum()函數在計算時,會忽略列值為NULL的行
?????? select? sum(列1) from 表名 where 條件 注:列= 值 // Sum是一個求和的函數,返回指定列值得總和
演示max函數的意思
?????? Select max(列1) from 表名; //max返回指定列中的最大值
演示min函數的意思
?????? select min(列1) from 表名; //min()返回列中的最小值
內連接查詢(inner join)
?????? Select 表名.列1,列2,列3 from 表名1,表名2 where 條件? 注釋:表名1和表名2的條件 ?
Select 表名.列1,列2,列3 from 表名1inner表名2 on條件
?//在內連接查詢中,只有滿足條件的記錄才能出現在結果關系中
左連接查詢(left join??? on)
?????? Select 表1,列1,列2,列3 from 表1 left join表2 on 條件; //以表1為標準沒有的話填充null
右鏈接查詢(right join??? on )
Select 表1,列1,列2,列3 from 表1 left join表2 on 條件; //以表2為標準沒有的話填充null
復合條件連接查詢
?????? Select 表1.列1,列2,列3 from 表1 inner join 表2? on 條件 and 條件;
帶any,some關鍵字的子查詢
?????? Select 列1 from 表名 where 列1 > any (select 列2? from 表2);
?????? Select 列1 from 表名 where 列1 > some(select 列2? from 表2);
?????? //any和some是同音字,表示滿足一個條件就行
帶all關鍵字的子查詢
?????? Select 列1 from 表名 where 列1 > all(select 列2? from 表2);//all需要滿足所有
帶exists關鍵字的子查詢
?????? Select 列名 from 表名 where exists (select 列名 from 表名? where 條件);
//先執行exists里的語句,如果里面的語句沒有答案就不執行外面的語句,如果有,則顯示
帶in關鍵字的子查詢
?????? Select 列1 from 表名 where 列2 in (select 列2 from 表名2 where 條件);
//in先執行內語句,之后用內語句的值提供給外語句進行比較操作
帶比較運算符的子查詢
?????? select 列1,列2 from 表名 where 列1= (select 表2.列3 from 表2 where 條件);
合并查詢結果 (union);
?????? Select 列1,列2,列3 from 表名 where 條件? union all select 列1,列2,列3 from 表名2 條件;? //union可以給多天select語句合并,不加上all作用是去重
為表取別名(表名 as 別名)
?????? Select 列名 from 表名 as 別名 where 條件;
為字段取別名(列名 as 別名)
?????? Select 列名 as 別名 , 列名2 as 別名 from 表名 where 條件;
正則表達式(regexp)
查詢以特定字符或字符串開頭的記錄 ^值
?????? Select 列名 from 表名 where 列名 regexp ‘^字符值’;
查詢以特定字符或者字符串結尾的記錄 值$
?????? Select 列名 from 表名 where列名 regexp ‘值$’
用符號‘.’來代替字符串中的任意一個字符
?????? Select 列名 from 表名 where 列名 regexp ‘值.值’
使用“*”和“+”來匹配多個字符
?????? Select?列名 from 表名 where 列名 regexp ‘值*’? //*是0到無數個
?????? Select 列名 from 表名 where 列名 regexp ‘值+’ ?//+是1到無數個
匹配指定字符串? |
?????? Select 列名 from 表名 where 列名 regexp ‘值|值’;
匹配指定字符中的任意一個
?????? Select 列名 from 表名 where 表名 regexp ‘[值1值2]’
匹配指定字符以外的字符
Select 列名 from 表名 where 列名? regexp ‘[^值]’
使用{n,}或者{n,m}來指定字符串連續出現的次數
?????? Select 列名 from 表名 where 列名 regexp ‘值{連續出的次數,最高多少次}’;
總結
以上是生活随笔為你收集整理的MySQL查询数据详细的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [css] rgba()和opacit
- 下一篇: oracle数据库应用中实现汉字“同音”