MySQL基本操作(二)

这篇博客详细介绍了SQL查询的基本语法,包括选择特定字段、筛选记录、使用WHERE子句、模糊匹配、统计函数以及排序和分页操作。还提到了如何进行多条件查询、删除和更新记录。对于数据库管理和数据分析人员来说,这些是必备的知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询语句

selete 语句

select 字段1,字段2,...字段n  from 表名 where [条件]
查询所有字段所有记录
select * from 表名;

‘*’ 统配标识符 匹配所有记录

查询(指定)部分字段的记录
select 字段1,字段2,...字段n  from 表名;
查询符合条件的记录

==条件语句:where ==

=  	等于
!=/<>      不等于
< 	小于
> 	大于
<= 	小于等于
>= 	大于等于
between.... and .... 	在...和...之间		也可以查询名字为xxx的(字符串匹配)
in(值1,值2......) 是否是其中的一个值

not in  与上面的正好不一样

like 相似 模糊匹配

1.%  百分号通配符  匹配任意长度的

select * from student where name like "H%";
2._ 下划线通配符  匹配单个字符的
select * from student where name like "H____";
查询空值

is null;
is not null;

select * from student where age is null;
select * from student where age is not null;
多个条件查询
and  相当于 &&
or 相当于 ||
去重 distinct

select distinct score from student ;
我看来有点鸡肋

排列查询(默认是升序排列asc)
select * from student order by score asc;
select * from student order by score desc;(降序排列)
指定查询数量

limit
(常见于网站的的查询页,反页)

select * from student limit 4;
查出按照 插入信息的顺序的前四个

select * from student limit 4,8;
查出按照插入信息的顺序的4-8个;
查询某个条件的记录数量:

count

select count(*) from student;
select count(*) as count(这里是命个名) from student;
select count(*) as count from student where[条件];
一些计算的函数

sum()

select sum(score) total from student;
select sum(score) as total from student;

avg()

select avg(score) avgs from student;

删除记录

delete from 表 where id=1;
删除id为1的记录

修改记录(更新数据)

updata

updata student set sex = ""  where [条件]
修改多个字段
updata student
set
sex="女";
age = 25;
while [条件];

不写条件就全改了,很危险
一定要写条件,条件不对也很危险

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值