MySQL基本查询语句
表示例
id | name | price | author | stock | img_path |
---|
1 | java从入门到放弃 | 80.00 | 国哥 | 7 | static/img/default.jpg |
2 | 数据结构与算法 | 78.50 | 严敏君 | 10 | static/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);