Mysql【查操作(上)】

mysql查操作(上)

select在sql中是很重要的操作了


use `school`;

-- 01. 查询所有学生的所有信息
select * from students;
-- *效率比较低-- 数据保存在sys中的cloum中的
-- 投影 
select stu_id
	,stu_name
	,stu_sex
	,stu_birth
	,stu_addr
	,col_id 
  from students;

-- 使用这个格式,更符合公司规定

-- 02. 查询学生的学号、姓名和籍贯(投影和别名)
select stu_id as 学号
	,stu_name as 姓名
	,stu_addr as 籍贯
  from students;
-- 03. 查询所有课程的名称及学分(投影和别名)-alias
select cou_name as 名称
	,cou_credit as 学分
from courses;
-- 04. 查询所有女学生的姓名和出生日期(数据筛选)
select stu_name
	,stu_birth
 from students 
 where stu_sex = 0;
-- 05. 查询籍贯为“四川成都”的女学生的姓名和出生日期(数据筛选)
select stu_name 
	,stu_brith 
 from students 
 where stu_sex = 0 
	and stu_addr = '四川成都';
-- 06. 查询籍贯为“四川成都”或者性别是女的学生(数据筛选)
select stu_name
 from students
 where stu_sex = 0
    or stu_addr = '四川成都';
-- 07. 查询所有80后学生的姓名、性别和出生日期(数据筛选)
-- 法一
 select stu_name 
	 ,stu_sex
	 ,stu_birth
 from students
 where stu_birth >='1980-1-1' and stu_birth <='1989-12-31';
 
 -- 法二
 select stu_name 
	 ,stu_sex
	 ,stu_birth
 from students
 where stu_birth between '1980-1-1' and '1989-12-31';
 
 -- mysql的方言
 select stu_name 
	 ,if(stu_sex,'男','女') as 性别
	 ,stu_birth as 出生日期
 from students
 where stu_birth between '1980-1-1' and '1989-12-31';
 
 -- 标准sql
 select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
    ,stu_birth as 出生日期
 from students
  where stu_birth between '1980-1-1' and '1989-12-31';
-- 08. 查询学分大于2分的课程名称和学分(数据筛选)
select cou_name
	,cou_credit
 from courses 
  where cou_credit>2;
-- 09. 查询学分是奇数的课程的名称和学分(数据筛选)
select cou_name
	, cou_credit
from courses
where cou_credit mod 2 <> 0;

-- 10. 查询选择选了1111的课程考试成绩在90分以上的学生学号(数据筛选)
select stu_id as 学生学号
	from records
    where cou_id = 1111
		and score > 90;
-- 11. 查询名字叫“杨过”的学生的姓名和性别
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
 from students
 where stu_name = '杨过';
 
-- 12. 查询姓“杨”的学生姓名和性别(模糊查询)
-- wild card —— 通配符(百搭牌)
-- %——匹配0个或任意多个字符
-- like 模糊匹配只针对字符串 

select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '杨%';
-- 13. 查询姓“杨”名字两个字的学生姓名和性别(模糊查询)
-- 下划线_ ——精确匹配一个字符
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '杨_';
-- 14. 查询姓“杨”名字三个字的学生姓名和性别(模糊查询)
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '杨__';
-- 15. 查询名字中有“不”字或“嫣”字的学生的姓名(模糊查询)
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '%不%'
   or stu_name like '%嫣%';
   
-- 法二:使用并集运算 
-- union 并集且去重,union all 并集但不去重 
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '%不%'
   union
select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
   from students
   where stu_name like '%嫣%';
   
-- 法三:使用正则表达式
select stu_name as 姓名
	from students
    where stu_name regexp '[\\u4e00-\\u9fa5]*?[不嫣][\\u4e00-\\u9fa5]*?';
    
-- 16. 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
-- 法一
select stu_name as 姓名
	from students
    where stu_name like '杨__'or stu_name like '林__';
-- 法二:正则表达式
select stu_name as 姓名
	from students
    where stu_name regexp '[杨林][\\u4e00-\\u9fa5]{2}';


update students set stu_addr = '  ' where stu_id = 3011;
-- 17. 查询没有录入籍贯的学生姓名(空值处理)
-- 三值逻辑 -true、false、unkown
-- trim —— 修剪字符串左右两边的空格 
-- 注意是is null或者是is not null
select stu_name
from students
where stu_addr is null or trim(stu_addr) ='';
-- 18. 查询录入了籍贯的学生姓名(空值处理)
select stu_name
from students
where stu_addr is not null or trim(stu_addr) <> '';

1.查询所有信息

方法1:select * from xxx;

因为sql数据是保存在sys中的cloum中的,如果*的话它就是先获取行在获取列,使用这个这个方法效率比较低,不推荐使用。

方法2:select stu_id
	,stu_name
	,stu_sex
	,stu_birth
	,stu_addr
	,col_id 
  from students;

使用投影的方法效率更高,并且在使用这个方法在实际工作中也更适用。

2.as别名

select stu_id as 学号
	,stu_name as 姓名
	,stu_addr as 籍贯
  from students;

3.数据筛选

使用where子句,在from子句后给出。

where操作符:
请添加图片描述

3.1检查单个值 =

3.2 不匹配检查 <>

3.3 范围值检查 between

3.4 空值检查 is null

3.5mysql的方言写法

 select stu_name 
	 ,if(stu_sex,'男','女') as 性别
	 ,stu_birth as 出生日期
 from students
 where stu_birth between '1980-1-1' and '1989-12-31';

这里的if和excle中的if函数是一个意思。

3.6 标准的sql写法

 select stu_name as 姓名
	,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
    ,stu_birth as 出生日期
 from students
  where stu_birth between '1980-1-1' and '1989-12-31';

CASE xxx when xxx then xxx when xxx then xxx else xxx end

,CASE stu_sex when 1 then '男'when 0 then  '女' else '未知' end as 性别
,stu_birth as 出生日期

from students
where stu_birth between ‘1980-1-1’ and ‘1989-12-31’;


CASE xxx when xxx then xxx when xxx  then xxx  else xxx  end 

case后跟需要判断的值,当xxx的时候就是xxx,当xxx的时候就是xxx,否则xxx,end结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值