初级SQL查询

1.查询所有列

select * from student;
查询表中所有的记录
注意:select后跟了*表示统配,就是所有的列。然而在实际开发中不建议大家使用*,因为会导致索引失败,

2.部分查询列

select id,name from student;

查询Student表中的id name

注意:查询的结果,被放入一个临时的表中;

3.为查询的列起别名

select name as 姓名,age as 年龄 from student;

as可以省略

select name 姓名,age 年龄 from student;

4.范围查询

select * from student where age<=50 and age>=19;
查询来自Student表中 条件是年龄50-19;
select * from student where age between 19 and 50;
等价与上面的

5.多条件查询

select *from student where age>=19 and address='上海';
and相当于逻辑与   or相当于逻辑或
select *from student where age>=19 or address='上海';
--如果我们又这样的需求要求查询年龄是 18或者20或者 30 或者36的学生
select * from student where age=18 or age=25 or age=30;
--下面的数据等价于上面的数据
select * from student where age in(18,25,30);

6.去重

select distinct address from student;
去掉地址重复的
使用distinct去掉重复字符段,要求查询的所有的列值,都必须相同;

7.模糊查询

(1)_表示一个字符

        select * from student where name like '_三';

(2)%表示任意字符

     
   select* from Student where name like '%三%';

8.排序

select * from student order by age desc;
order by以...排序    desc倒叙  asc升序
升序默认不写 asc
--根据多列排序
select * from student  order by age desc, age;

注意:既有 where 又有order by那么谁在前 谁在后

where在前 order by在后

回顾

1.手机号在企业开发时,一定用的都是字符串类型

2.创建表结构 commit表示注释 int不建议大家定义长度,因为如果长度大于可以用long

3.添加数据事件类型做添加数据时也是用单''号

4.查询年龄别为空的学生的信息 is null 判断为空 is not null 判断不为空

5.查询学分在2-4之间课程的信息 between 2 and 4;

正文:

1.查询列上进行查询

select age*3 name from student;

在实际开发中订单的总价可以通过订单的价格*订单数量计算出来,就没有必要在数据库中在定义订单的总价了

2.聚合函数

        SQL结构化查询语言,也是一种编程语言,所以也有函数,并且有内置函数(官方)和自定义函数(程序员);

聚合函数一共有5个:

max(列名)求某列的最大值;

select max(credit) from c1;

min(列名)求某列的最小值;

select min(credit) from c1;

sum(列名)求某列的和;

select sum(credit) from c1;

avg(列名)求某列的平均值;

select avg(credit) from c1;

count(列名)求某列的个数;

select count(credit) from c1;
count(*)表示的列中的所有个数列
count(1)表示表中第一个列但是他不能使用distinct

3.分组查询

        在SQL中有个 group by 语句,将某一列相同数据视为一组,然后进行查询

注意:通常与聚合函数连用;如果使用了group by那么select后只能跟分组条件列和聚合函数;

select id,address,count(*),avg(age),max(age) from student group by address having max(age)>30;
这是出错误了,为什么?
因为select后跟了id,而且id不是分组条件
--查询 人数大于3的地区 最大年龄
select address,max(age) from student group by having count(*)>3;
--查询每个地区 25岁以上人数的数量
select address,count(*)from student where age>25 group by address;
--找出年龄最大的学生姓名
select name from student where age=(select max(age) from student);

having是分组之后查询的

4.分页查询

        当数据库表数据比较大时 执行 select*from Student此时数据库有可能会卡死如果在java中,有可能内存直接溢出,所以实际开发中,都是分页查询

        分页的使用:limit

select * from Student limit 3,5;--表示从第三条记录查询,查询5条记录;
--分页表示表示的公式
select * from 表名 limit (n-1)*m,m;--表示第n页得m条记录;

6.sql的优先级

(1)SQL语法:

select distinct * from 表名
      where 条件;
      group by 分组;
      having 分组条件;
      order by 排序;
      limit 分页;

        (2)mysql执行的顺序

from 先确定从那个表中获取数据,所以先执行 from
where: where 语句是对条件加以限定
group by:分组语句
聚合函数;
select语句
distinct 去重
order by
limit

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值