20241226_144124 mysql 数据行操作 查询操作 必会语句

查询表格的所有数据
select * from 表名

查询tb表的所有数据
select * from tb

查询表格的所有数据,只显示限定的列
select 列1,列2 from 表名

查询student表的全部数据,只显示id与name列
select id,name from student

查询表格的数据,显示限定的列,并给列起别名
select 列1 as 别名1,列2 as 别名2 from 表名 where 条件

查询student表的全部数据,只显示id与name列,给id列起别名为学号,给name列起别名为姓名
select id as 学号, name as 姓名 from student

去重查询,查询数据表的所有信息,只显示限定的列,并对该列去重
select distinct 列名 from 表名

查询student表中的学生都来自哪个城市的,显示城市名。提示:对city列进行去重查询
select distinct city from student

条件查询,查询符合条件的所有数据
select * from 表名 where 条件

查询student表中分数score小于等于60分的所有数据
select * from student where score <= 60

查询student表中性别gender列 不为 '保秘’ 的所有数据
select * from student where gender != ‘保秘’

条件查询,成员查询
列名 in (值1,值2,值n)
列名 not in (值1,值2,值n)

查询student表中城市city列是 ‘武汉’ 或者 ‘长沙’ 的所有数据 提示:成员运算符
select * from student where city in (‘武汉’,‘长沙’)

查询student表中城市city列 即不是 ‘武汉’ 也不是 ‘长沙’ 的所有数据 提示:成员运算符
select * from student where city not in (‘武汉’,‘长沙’)

条件查询,范围查询
列名 between 值1 and 值2
注意:
值1要小于值2
可以用于数值的范围查询与日期的范围查询

查找student表中分数score大于80分并且年龄age<16岁的数据
select * from student where score>80 and age<16

查询student表,获取全部分数score大于80或者地址place在武汉的数据
select * from student where score>80 or place=‘武汉’

查找people表,获取年龄age在20-30岁的所有数据。包含年龄等于20与等于30的数据
select * from people where age between 20 and 30

条件查询,模糊查询
列名 like 表达式
注意:
表达式主要由,百分号,下划线组成

查找hero表,找出名字name为两个字的所有数据
select * from hero where name like ‘__’

查找hero表,找出名字name中包含 ‘白’ 的所有数据
select * from hero where name like ‘%白%’

查找hero表,找出所有姓名name以张开头的数据
select * from hero where name like ‘张%’

分页查询,限定显示的结果
limit n
limit m,n

查找hero表,在所有数据中只显示前三条
select * from hero limit 3

查找hero表,在所有数据中只显示从下标从2开始的5条数据
select * from hero limit 2,5

查询student(id,name,score)表,只显示name与score字段与一个名为rating的字段,如果学生的score在90到100分,rating值为A;如果score在60到90间,rating值为B,如果score值比60还要小,rating值为C
SELECT
name,
score,
CASE
WHEN score >= 90 AND score <= 100 THEN ‘A’
WHEN score >= 60 AND score < 90 THEN ‘B’
WHEN score < 60 THEN ‘C’
END AS rating
FROM student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鲸鱼编程pyhui

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

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

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

打赏作者

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

抵扣说明:

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

余额充值