1.查詢表中所有信息
select * from 表
2.查詢指定的列
select 列名1,列名2, ......
from 表
3.查詢某屬性前5條記錄
select top 5 列名1,列名2, .....
from 表名
4.查詢某列信息並且用 distinct 校消去重復記錄
select distinct 列名1
from 表名
5.更改列標題
-采用“列標題=列名”
-采用“列名 列標題”
-采用“列名 as 列標題”
6.使用計算列,對整列數據加減乘除
select 列名
運算式
from 表名
7.聚合函數
①平均
select avg(列名)
from 表名
②返回表中記錄數
select count(*)
from 表
8.求和(只能用於數字列)
sum(列名)
9.求最小最大值
min()
max()
10.查詢范圍之between
between and 不同數據庫對邊界包含情況是不同的
select *
from 表名
where 列名
between v1 and v2 --不介於12之間:not between v1 and v2
11.查詢范圍之in
select *
from 表名
where 列名 in(1,5,7)
12.like模糊匹配
select *
from 表名
where 列名 like ‘李%’ --帶李字的
‘_楊’ --名字是兩個,后邊的字是楊
‘_枝%’ --名字中第二個是枝
‘[^李]%’ --不姓李
13.聚合函數
count(*)
--統計元組個數
14.排序(desc是降序,asc是升序)
select *
from 表名
order by 列名 desc/asc,列名 desc/asc --這是有次序的
15. group by的用法
select 列名1,avg/sum(列名2),....
from 表名
group by 列名1
------------------------注意,select語句是最后執行的----------------------
GROUP BY我們可以先從字面上來理解,GROUP表示分組,BY后面寫字段名,就表示根據哪個字段進行分組(解釋來自簡書Awesome_Tang)
16.查詢連接
select*
from 表名1,表名2