MySQL 基本查询语句

本文详细介绍了MySQL的基本查询语句,包括选择单个或多个列、检索所有列、获取唯一行、限制结果数量、排序数据、使用WHERE子句、操作符、函数以及子查询等。还涉及到了高级查询技巧,如正则表达式、计算字段、别名、数据处理函数以及分组和过滤。内容覆盖了从基础到进阶的各种查询场景,对于理解和掌握MySQL查询非常有帮助。
摘要由CSDN通过智能技术生成

MySQL基本查询语句

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bRwVul9m-1647533317062)(C:\Users\艾虎\Desktop\SELECT各种形式.jpg)]

表示例

idnamepriceauthorstockimg_path
1java从入门到放弃80.00国哥7static/img/default.jpg
2数据结构与算法78.50严敏君10static/img/default.jpg

检索单个列

select name
from t_book;

检索多个列

select name, price
from t_book;

检索所有列

select *
from t_book;

检索不同的行

select distinct img_path
from t_book;

限制结果

select name
from t_book
limit 5;

使用完全限定的表名

select t_book.name
from t_book;

排序数据

select name
from t_book
order by name;

按多个列排序

select name, price
from t_book
order by price, name;

指定排序方向

select name, price
from t_book
order by price desc;
#desc为降序,默认为升序

使用where子句

select name
from t_book
where id = 1;
# 同时使用order by和where子句时,应该让order by位于where之后,否则会产生错误

where子句操作符

# = 等于
# <> 不等于
# != 不等于
# < 小于
# <= 小于等于
# > 大于
# >= 大于等于
# between 在指定的两个值之间

检查单个值

select name
from t_book
where id = 1;

不匹配检查

select name
from t_book
where id <> 1;
# select name
# from t_book
# where id != 1;

范围值检查

select name
from t_book
where id between 1 and 5;

空值检查

select name
from t_book
where stocks is null;

and操作符

select name, price
from t_book
where price > 100 and stock > 1;

or操作符

select name, price
from t_book
where price > 100 or stock > 800;

计算次序

select name, price
from t_book
where id > 5 or price > 100 and stock > 800;
# 任何时候使用具有and和or操作符的where子句,都应该使用圆括号明确地分组操作符

in操作符

select name, price
from t_book
where id in (2, 8)
order by price;
# in-->where子句中用来指定要匹配值的清单的关键字,功能与or相当

not操作符

select name, price
from t_book
where id not in (2, 8)
order by price;

like操作符

百分号(%)通配符

select name, price
from t_book
where name like 'C%';
# %C C%C %匹配一个或多个字符

下划线(_)通配符

select name, price
from t_book
where name like 'J_va编程思想';
# _ 匹配一个字符

使用通配符的技巧

# 不要过度使用通配符。如果其他操作符能达到相同的目的,应该使用其他操作符。
# 在确实需要使用通配符时,除非绝对有必要,否则不要把它们用在搜索模式的开始处。把通配符置于搜索模式的开始处,搜索起来是最慢的。
# 仔细主义通配符的位置。如果放错地方,可能不会返回想要的数据。

使用MySQL正则表达式

基本字符匹配

select name, price
from t_book
where name regexp 'J.va编程思想';
# . 匹配任意一个字符

进行or匹配

select name, price
from t_book
where name regexp '蛋炒饭|赌神';
# | 匹配其中之一

匹配几个字符之一

select name, price
from t_book
where name regexp '[JCU]ava编程思想';
# [] 匹配[]中任意一字符

匹配范围

select name, price
from t_book
where name regexp '[1-9]ava编程思想';
# [1-9] 匹配任意数字 [a-z] 匹配任意字母字符

匹配特殊字符

select name, price
from t_book
where name regexp '\\.';
# \\. 表示查找.

拼接字段

select concat(name, '(', price, ')')
from t_book
order by price;

使用别名

select concat(name, '(', price, ')') as title
from t_book
order by price;

执行算术运算

select name,
       price,
       stock,
       price * stock as total_price
from t_book
where id = 2;

使用数据处理函数

文本处理函数

# left() 返回串左边的字符
# length() 返回串的长度
# locate() 找出串的一个子串
# lower() 将串转换为小写
# ltrim() 去掉串左边的空格
# right() 返回串右边的字符
# rtrim() 去掉串右边的空格
# soundex() 返回串的SOUNDEX值
# substring() 返回字串的字符
# upper() 将文本转换为大写

日期和时间处理函数

# adddate() 增加一个日期(天,周等)
# addtime()	增加一个时间(时,分等)
# curdate()	返回当前日期
# curtime() 返回当前时间
# date()	返回日期时间的日期部分
# datediff()	计算两个日期之差
# date_add()	高度灵活的日期运算函数
# date_format()	返回一个格式化的日期或时间串
# day()	返回一个日期的天数部分
# dayofweek()	对于一个日期,返回对应的星期几
# hour()	返回一个时间的小时部分
# minute()	返回一个时间的分钟部分
# month()	返回一个日期的月份部分
# now()	返回当前日期和时间
# second()	返回一个时间的秒部分
# time()	返回一个日期时间的时间部分
# year()	返回一个日期的年份部分

数值处理函数

# abs()	返回一个数的绝对值
# cos()	返回一个角度的余弦
# exp()	返回一个数的指数值
# mod()	返回除操作的余数
# pi()	返回圆周率
# rand()	返回一个随机数
# sin()	返回一个角度的正弦
# sqrt()	返回一个数的平方根
# tan()	返回一个角度的正切

avg()函数

select avg(price)
from t_book;
# 求平均值

count()函数

select count(*)
from t_book;
# 计数

max()函数

select max(price)
from t_book;
# 最大值

min()函数

select min(price)
from t_book;
# 最小值

sum()函数

select sum(price)
from t_book;
# 和

创建分组

select name, price
from t_book
group by id;

过滤分组

select name, price, count(*)
from t_book
group by id
having count(*) >= 1;
# having支持所有where操作符

select子句顺序

# select
# from
# where
# group by
# having
# order by
# limit

子查询

in关键字的子查询

select * from t_book where bookType in(select id from t_bookType);

select * from t_book where bookType not in(select id from t_bookType);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

热河路的IT男

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

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

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

打赏作者

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

抵扣说明:

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

余额充值