MySQL查询操作

本文介绍了MySQL的基本查询语句,包括如何使用SELECT语句查询指定字段、使用表达式进行计算、利用DISTINCT实现数据去重、利用WHERE子句进行条件筛选、进行多条件组合查询等实用技能。

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

MySQL查询语句:

select * from 表名; ##查询出该表名下所有数据

*代表所有字段

简单的查询语句方式

select [字段列表 / 表达式 / 函数]  from 表名;

查询多个字段

select 字段1,字段2 from 表名;

表达式:

select 表达式[算术表达式] from 表名;

例如:此处有一张工资表 payroll:其中包含2个字段:

name && wages

查询一年的工资:

select name,wages*12 from payroll;

去重:
在需要去重的字段前加上 distinct
例如:test表中有多个相同数据字段名为:tt

select distinct tt from test;

函数:

where条件查询:

where后面一般跟表达式(主要是条件表达式)
条件表达式可以是 等值比较 大于 小于 大于等于 小于等于 不等于
where 条件表达式

语法:

select * from 表名 where 字段 = 字段值;

例1:
book中有3本书 price 都为10

select * from book where price = 10;

例2:
查询book表中price小于10的书籍:

select * from book where price < 10;

例3:
查询book表中price大于10的书籍:

select * from book where price > 10;

例4:
查询book表中price不等于10的书籍:

select * from book where price <> 10;

多条件查询:

并且 关键字 and
或者 关键字 or
in关键字 in 代表在这个取值中只要有一个匹配符合条件;
not in 不在这个范围区间之内的;
查询book表中大于10且小于20的书:

select * from book where price >10 and price < 20;

查询book表中大于10且小于20,并且日期为2010-9-10的书:

select * from book where price >10 and price < 20 and date = '2010-9-10';

查询book表中大于20的或者小于10的:

select * from book where price < 10 or price > 20;

in关键字 in 代表在这个取值中只要有一个匹配符合条件;相当于 多个 or 条件

select * from book where price in(10,20,30);

not in 不在这个区间范围内:

select * from book where price not in(10,20,30);

between ... and .... 相当于大于等于 小于等于

select * from book where price between 10 and 20;

在mysql中 NULL 不等于 空 也就是 price 不能等于 null 不能这样查询
判断一个字段的数值是否为空,需要用到关键字 is;
判断不为空 需要用到关键字 not is

例如 查询免费书籍,也就是 price 为null

select * from book where price is null;

##不能写成 select * from book where price = null;

查询字符串长度

select length(字符串名) from 表名 ;

例:

select length(name) from book where num = 1 ;

查询num为1 的name字段长度;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

指剑

捐点钱吧,小笼包8元一笼,谢谢

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

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

打赏作者

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

抵扣说明:

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

余额充值