Mysql數(shù)據(jù)庫之常用sql語句進階與總結(jié)
本文實例講述了Mysql數(shù)據(jù)庫之常用sql語句。分享給大家供大家參考,具體如下:
前面講述了Mysql sql基本語句。這里繼續(xù)總結(jié)一下SQL語句的進階內(nèi)容。
SQL語句進階
1.查詢字段:
————查詢所有字段
select * from 表名;
————查詢指定字段
select 字段名,字段名… from 表名;
————多數(shù)據(jù)表連接查詢時
select 表名.字段名,表名.字段名 … from 表名;
————使用as給表起別名
select 表別名.字段名 from 表名 as 表別名;
————消除重復(fù)行(distinct)
select distinct 字段名 from 表名;
2.條件查詢:
————比較運算符(>,<,=,!=)
select * from 表名 where age >18;
(<>也表示!=)
————邏輯運算符(and,or,not)
select * from 表名 where age>18 and age<28;(18
3.排序:
————升序
select * from 表名 order by asc;(默認(rèn)為升需asc,可以省略asc)
————降序
select * from 表名 order by desc;
4.聚合函數(shù):
————總數(shù)count
select count(*) from 表名;
————最大值max
select max(age) from 表名;
————最小值min
select min(age) from 表名;
————求和sum
select sum(age) from 表名;
————求平均值avg
select avg(age) from 表名;
————四舍五入保留小數(shù)round
select round(avg(age),2) from 表名;(查詢平均年齡,四舍五入保留兩位小數(shù))
5.分組(重點):
————分組group by
select gender count(*) from 表名 group by gender;(按性別分組,查詢性別與人數(shù))
————分組查詢(聚合函數(shù),group_concat(),having)
select gender avg(age) from 表名 group by gender;(查詢每種性別的平均年齡) select gender group_concat(name) from 表名 group by gender;(group_concat(name)查看分組姓名) select gender count() from 表名 group by gender having count()>2(having類似where,過濾條件,having只能用于group by,where用于表數(shù)據(jù))
————匯總with rollup
select gender count(*) from 表名 group by gender with rollup;(最后新增一行,顯示匯總結(jié)果)
6.分頁:
————查詢前n個數(shù)據(jù)(limit一般寫在最好,表示對操作后的數(shù)據(jù)顯示)
select * from 表名 limit n;
————分頁顯示
select * from 表名 limit 0,3;(每頁顯示3個,第1個頁面) select * from 表名 limit 3,3;(每頁顯示3個,第2個頁面) select * from 表名 limit 6,3;(每頁顯示3個,第3個頁面)
7.連接查詢(重點):
————inner join…on(內(nèi)連接)
select * from 表名1 inner join 表名2 on 表名1.cls_id=表名2.id;(將表1cls.id和表2id相同的連接在一起) select 表名1.字段名1,表名2.字段名.2 from 表名1 inner jion 表明2 on 條件;
————left/right join…on(左/右/外連接)
select * from 表名1 left/right join 表名2 on 表名1.cls_id=表名2.id;(查詢的結(jié)果為兩個表匹配到的數(shù)據(jù)和左表特有的數(shù)據(jù),對于左/右表中不存在的數(shù)據(jù)使用null填充)
8.子查詢:
————標(biāo)量子查詢(子查詢返回的結(jié)果是一個數(shù)據(jù)(一行一列))
select * from 表名 where age > (select avg(age) from 表名);
————列子查詢(返回的結(jié)果是一列(一列多行))
select name from 表名1 where id in (select cls_id from 表名2);
————行子查詢(返回的結(jié)果是一行(一行多列))
select * from 表名 where (height,age) = (select max(height),max(age) from 表名);
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
相關(guān)文章
Mysql及Navicat中設(shè)置字段自動填充當(dāng)前時間及修改時間實現(xiàn)
這篇文章主要給大家介紹了關(guān)于Mysql及Navicat中設(shè)置字段自動填充當(dāng)前時間及修改時間實現(xiàn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-07-07Mysql數(shù)據(jù)庫清理binlog日志命令詳解
這篇文章主要給大家介紹了Mysql數(shù)據(jù)庫清理binlog日志命令的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Mysql具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09與MSSQL對比學(xué)習(xí)MYSQL的心得(一)--基本語法
最開始接觸的數(shù)據(jù)庫為MSSQL,不過最近項目需求,仔細學(xué)習(xí)了下MYSQL,下面就對比MSSQL,把MYSQL的學(xué)習(xí)心得分享給大家2014-06-06