SQL简单查询语句
1.總體查詢
select * from 表名
2.where查詢
a.and or查詢
select * from 表名 where? ?條件1 and 條件2? ? ?
(條件1和條件2同時滿足)
select * from 表名 where 條件1 or 條件2
(條件1和條件2只要一個滿足)
注意:and 的優先級高于or 即:
select * from 表名 where 條件1 or 條件2 and 條件3?
(優先執行條件2和條件3,若是要先執行條件1和條件2的關系,則加一個括號就好)?
b. like in 查詢
like也稱為模糊查詢
select * from 表名 where 列表 like "%x%"
(查詢出表中含有字段x的列表)
select * from 表名 where 列表 like "%x"
(查詢訊出表 以x結尾的列表)
select * from 表名 where 列表 like "x%"
(查詢訊出表中以x開頭的列表)
select * from 表名 wher e 列表 like "_x"
(查詢訊出表中以x為第二個字符開頭的列表)
select * from 表名 where 列表 like "x_"
(查詢訊出表中以x為最后兩個字符結尾的列表)
in查詢
select * from 表名 where 列表 in (條件1,條件2)
(in是可以替代or,和or的關系是一樣的)
注意:like 和 in 均可以反向操作,即在他們的前面加一個NOT
c.正則表達式(regexp)
select * from 表名 where 列表 regexp "^[A-Z]"
(查詢出以大寫字母開頭的列表)
select * from 表名 where 列表 regexp "^[a-z]"
(查詢出以小寫字母開頭的列表)
select * from 表名 where 列表 regexp "[漢字]" ? ?
(查詢出含有漢字的列表)
select * from 表名 where 列表 regexp "^[0-9]"
(查詢出以數字開頭的列表)
select * from 表名 where 列表 regexp "[A-Z]s"
(查詢出以大寫字母結尾的列表)
d.去重(distinct)
select distinct??* from 表名 where 條件
(去除篩選出來出現重復的)
select distinct? 字段1,字段2? from 表名 where 條件
(去除字段1和字段2出現重復的)
e.改名(as)
select 字段1 as 新字段1 , 字段2 as 新字段2 from 表名?
select 字段1 新字段1 , 字段2 新字段2?
(在修改列名的時候可以將as省去)
f.限制數據顯示的數量(limit)
select * from 表名 where 條件 limit n,m
(n代表開始的行的數,m代表能夠顯示的數量)
g.排序 (order by)
select * from 表名 where 條件 order by 字段? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? (表中字段按照從低到高排列)
注意:order by 默認的排列方式為從低到高,若是要從高到低則在字段后面加desc
3.函數
a.統計(count)?
select count(*) from 表名
(統計表中所有的行數)
select count(1)? from? 表名
(統計表中所有的行數)
注意:以上兩者的能達成一樣的效果,但是count(1)效果更好,速度更快
select count(字段名) from 表名
(統計表中不為空的行數)
b.最大值(max)和最小值(min)
select max(字段),min(字段) from? 表名
c.平均值(avg)和求和(sum)??
select avg(字段),sum(字段) from 表名
4.分組(group by)
select * from 表名 group by 字段
(按照字段分組)
select * from 表名 group by 字段 having 條件
(篩選出滿足條件的組)
注意:分組后的信息只能顯示某個具體的信息,但不能顯示組內所有信息
大體語句:select * from 表名 where 條件 group by having 條件
5.多表查詢(join)
a.內連接
select * from 表名1? inner join 表名2 on 表名1.字段=表名2.字段
b.外鏈接? ? ?
左連接
select * from 表名1 as 表名1? left join 表名2 as 表名2?on 表名1.字段=表名2.字段? ?
( 以表1為準,左右兩張表通過字段相同的鏈接在一起,但若是表1中有表2存在的字段,則還是會出現,不過某些字段卻為NULL,若是表2存在表1的字段則會自動pass掉 。同時里面的as+表名也是可以省略不寫)
右連接
select * from 表名1 as 表名1? right join 表名2 as 表名2?on 表名1.字段=表名2.字段? ?
全連接
select * from 表名1 as 表名1? full join 表名2 as 表名2?on 表名1.字段=表名2.字段?
6.子查詢或者叫嵌套查詢(in)
select * from 表名1 where 字段1 in (select 字段1 from 表名2 where 字段2 in? (select 字段2 from 表名3 where 條件))? ?
其等效的效果為:
select * from 表名1 inner join 表名2 on 表名1.字段1=表名2.字段1 inner join 表名3 on 表名2.字段2=表名3.字段2 where 條件
注意:在使用的時候,盡可能的使用英文,因為中文可能會出現亂碼情況。且in會出現長度限制,但inner join卻不會,而且inner join查詢的結果可能會更多更全面,這個要根據自己需要
7.連接查詢(union all)
select 字段1 ,字段2,"xx"? from 表名1
union all
select 字段1 ,字段2,"xx" from 表名2 (將兩張表連接起來,且表頭為第一張的字段)
注意:在使用union all的時候一定要保證兩張表所查詢出來的字段數量相等,且select all默認不去重 ,若是需要去重則講all去掉,若是要將查詢出來的字段標記則是需要在字段x的后面加逗號和雙引號,引號里面則是你想要備注的值
8.時間查詢(date)
select? now();
(查詢當前時間)
select current_timetamp
(查詢當前時間)
注意:兩者是一樣的,只不過是不同版本
select? date_sub(now(),interval 1month)
(按照當前時間提前一個月)
select date_sub('2017-9-15',interval 1minth)
(查詢2017-8-15 即按照2017-9-15提前一個月)
select date_format(now(),"%Y")
(格式化當前時間至年這個點)
select date_format(now(),"%y")
(格式化當前時間至年這個點)
注意:這兩個的差別是前面y一個輸出4位(2017)后面則輸出兩位(17)
select date_format(now(),'%m')
(格式化當前時間的月,輸出為9)
select date_format(now(),'%d')
(格式化當前時間的日,輸出為15)?
select date_format(now(),'%T')
(格式化當前時間的時間,輸出為13:52:12)
select date_format(now(),'%Y-%m-%d-%T')
(格式化當前時間的月,輸出為2017-9-15-13:52:12)
?
?
?
? ? ?
??
??
?
轉載于:https://www.cnblogs.com/dxc159753/p/SQL.html
總結
- 上一篇: U盘出现问题不能读取怎么 U盘读取失败解
- 下一篇: linux cmake编译源码,linu