SQL语句-查询

查询Select

简单查询

查询所有列:

select * from tablename;

查询指定列:

select 列名1,列名2,……  from tablename;

去重,返回唯一不同的值,若有多列,只要不全同皆返回

select distinct 列…… from tablename;

条件选择where

select * from tablename where id=1;

比较条件和逻辑条件:

比较:=,>,<,>=,<=,!=
逻辑 :and,or,not

select * from tablename where id=1 and name='cj''l';
select * from tablename where id=1 or name='cjl';
select * from tablename where not id=1;

字符串

用单引号’ ‘括起来,若要在字符串中用单引号’,在字符串中两个单引号’'表示一个单引号
isn’t

'isn''t'

运算符的优先级:

()—> not —> and —> or

特殊条件:

判空

is null

select id from tablename where name is null;
select id from tablename where name not is null;
区间

between,区间时左闭右开,[1,10)

select * from tablename where id between 1 and 10
select * from tablename where id not between 1 and 10

字母区间:以’A’到’H’之间字母开头的所有name

select * from tablename where name between 'A' and 'H'

日期区间

select id from tablename where datename between '2020-07-06' and '2020-07-08';
in
select * from tablename where id in(1,2,3,4);
模糊查询like

% : 代表0个或多个字符
_ : 代表一个字符

like 'M%'  //以M开头的所有
like '%M'  //以M结尾的所有
like '%M%' //包含M的所有
like '_oogle' //任意字符+oogle的所有

排序order by

默认:升序
desc:降序

selec * from tablename order by id;
selec * from tablename order by id desc;

多列排序
先按第一个列排序,第一列一样的再对第二列排序

select * from tablename order by id,age;
select * from tablename order by id desc,age;
select * from tablename order by id,age desc;

分页查询

limit:返回规定数目的记录
返回前五条记录

select * from tablename limit 5;

返回id最大的前五条

select * from tablename order by id desc limit 5;

分页查询重点:limit startIndex,length
startIndex:起始位置
length:要查询记录的数目
意思:从startIndex的位置开始查询length条记录

查询前五条记录

select * from tablename limit 0,5;

查询第6到10条记录

select * from tablename limit 5,5;

别名 as

对列名分别取别名

select1 as 别名1,列2 as 别名2,…… form tablename;

将不同列结合取别名

select1 as 别名1,contact(2,',',3,',',……)as 别名2 from tablename;

表的别名

select1别名.列,表2别名.from1 as 别名1,表2 as 别名2 where ……
select w.name,a.date from1 as w,2 as a where w.id=a.id;

合并union

将两个或多个select语句的结果合成一个结果集
要求:必须拥有相同数量的列,对应的列也必须是相同的数据类型
表1:

idname
1aa
2bb
3cc

表2

agestring
22dd
22ee
23ff
select id,name from tab1
union
select age,string from tab2

union结果集中列名是第一个select语句中的列名

idname
1aa
2bb
3cc
22dd
22ee
23ff

union默认去除重复

select id from tab1
union 
select age from tab2
id
1
2
3
22
23

要显示全部使用union all

SQL函数

聚合函数

聚合函数:计算从列中取得的值,返回一个单一的值
聚哈函数不能出现在where当中

  • AVG():返回平均值
  • count(): 返回行数
  • first():返回第一个记录的值
  • last():返回最后一个记录的值
  • Max():返回最大值
  • Min():最小值
  • Sum():总和

当多行函数遇到NULL时会自动忽略,而若将NULL用作计算,那么结果总是NULL,如800+NULL=NULL
count(*)和count(id)的区别

  • count(*)统计总记录的条数
  • count(id)统计的非NULL的总量
select count(*) from tab;
select count(id) from tab;

分组group by

group by经常用于结合聚合函数,根据一个或多个列对结果集进行分组

select age,count(*) from tab group by age

having

having用于对分组之后的数据进行再次过滤

执行顺序
select……(5) from……(1) where……(2) group by……(3) having……(4) order by……(6)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值