sql常用查询语句

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

   

原文地址:https://www.cnblogs.com/-XiangBei-/p/11791579.html