mysql数据管理系统(3)

本文详细介绍了SQL的基础查询操作,包括字段查询、模糊查询、指定条件查询、各种类型的连接查询(内连接、左连接、右连接、全连接)以及子查询。此外,还涉及到数据的排序、分组、空值处理以及使用函数。同时,讲解了如何创建视图以及使用索引来优化查询性能。内容全面,适合数据库初学者和进阶者学习。
摘要由CSDN通过智能技术生成

##字段查询
select * from students;
select  s_name as sn(别名) from students;
select s.s_name as sn from students as s(别名);

#模糊查询  like
select * from students   where  s_name like '%o%';
#指定条件查询
select * from mins where age>18 and sex = '男’;

#内连接  inner join
select * from department inner join students
select d_id,d_name from department , students
#内连接条件查询  
select * from department as d inner join students as s
  on d.d_id = s.dept_id

#左连接 小表放左边作主表
select d.d_id,d.d_name,s.s_name from department as d left join 
students as s on d.d_id =s.dept_id
#右连接                                                                                    right
 #全连接  去重
select * from department as d left join students as s on d.d_id=
s.dept_id
union
select * from department as d right join students as s in d.d_id=
s.dept_id
  
#子表查询
select * from department left join(
select  dept_id,s_name,s_age,s_sex  from students left join
stu_detail on students.s_id =stu_detail.stu_id) as e on e.dept
_id =department.d_id
#子表条件查询
select * from students where dept_id =(
   select d_id from department where s_name='java');
 
#排序
select * from mins order by age desc;
                                                          asc
#select * from mins order by age dsec limit 4 
                                                                            1,4
#分组
select d_id,d_name,count(*) from students
      inner join department on 
      students.dept_id = department.d_id
      group by d_id,d_name ;
                                                having count(*)=2

#处理空值
select s_name.ifnull(dept_id,1) from students
#左截取
select s_name,left(s_name,2) from students;
                          right
                         substring
#添加索引
alter table mins add index(age)

##mysql 函数
#最大值
select max(age) from mins;
           min 
           avg
       
#视图
create viem stu_det as select s.s_name, d.d_id from department
as d left join students as s on s.dept_id = d.d_id left  join stu_de
tail as stu on s.s_id = stu.stu.id;
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

败北:)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值