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);

MySQL 基本查询语句相关推荐

  1. mysql 查询语句 参数,mysql参数化查询语句有关问题

    mysql参数化查询语句问题 部分代码如下: using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.O ...

  2. MySQL高级查询语句——超详细,一篇就够了

    MySQL高级查询语句 一.MySQL进阶查询 1.1.按关键字排序--order by 1.2.对结果进行分组--group by 1.2.1.单个分组 1.2.2.分组结合order by使用 1 ...

  3. mysql中如何分页查询_MySQL_mysql分页原理和高效率的mysql分页查询语句,以前我在mysql中分页都是用的 l - phpStudy...

    mysql分页原理和高效率的mysql分页查询语句 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我 ...

  4. Mysql高级查询语句练习

    Mysql高级查询语句练习 DROP TABLE IF EXISTS `tblcourse`; CREATE TABLE `tblcourse` ( `CourseId` varchar(3) NOT ...

  5. 为什么MySQL做查询语句时,第一次会很慢,但是第二次,第三次就会变快

    为什么MySQL做查询语句时,第一次会很慢,但是第二次,第三次就会变快 为什么MySQL的查询事务第一次执行会很慢,第二次,第三次就会快很多呢? 在国外,有个老外这么提问 Hi, I have an ...

  6. 【SQL】MySQL的查询语句

    文章目录 SELECT语句 WHERE子句 JOIN语句 GROUP BY和HAVING ORDER BY LIMIT 其他关键字 MySQL是一种广泛使用的关系型数据库管理系统,它被广泛地应用于各种 ...

  7. MySQL数据查询语句

    MySQL数据查询语句 MySQL 表单查询是指从一张表的数据中查询所需的数据,主要有查询所有字段.查询指定字段.查询指定记录.查询空值.多条件的查询.对查询结果进行排序分组等. 查询结构 SELEC ...

  8. mysql命令查询语句

    1.单表查询 select * from student; 采用*效率低,不推荐,多用列名 一.单表查询的语法:SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fi ...

  9. mysql 查询语句_SQL语言mysql基础查询语句

    单表查询.条件查询.查询并排序.限制结果查询.查询并排名.分组聚合查询.······ -- DQL操作,数据基本查询语言使用-------------------------------------- ...

最新文章

  1. R语言聚类分析--cluster, factoextra
  2. Spring3 MVC 注解---注解基本配置及@controller和 @RequestMapping 常用解释
  3. 特斯拉撞了警车:辅助系统Autopilot全程开启,连撞两车还没自动停下
  4. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)...
  5. 【java的多态性】
  6. java修改文件的大小限制_Struts2修改上传文件大小限制方法解析
  7. mysql 远程攻击_gopher 协议攻击内网 mysql
  8. 自定义View之onMeasure()
  9. Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
  10. mac 雪豹 10.6 五国
  11. CentOS如何挂载硬盘
  12. 云网管—云上构建网络自动化体系
  13. pycharm与webstorm 2017 激活破解
  14. 信息学奥赛一本通(1314:【例3.6】过河卒(Noip2002))
  15. 《数据库技术原理与应用教程第2版》——3.6计算机世界与物理模型
  16. [转]nginx常见配置详解
  17. java 多线程局域网快速传输文件,java大文件复制最高效方法多线程FileChannel
  18. Linux--RAID磁盘阵列与阵列卡
  19. 使用腾讯云托管部署前端项目
  20. Android Sunflower 带您玩转 Jetpack

热门文章

  1. 真正的标准化机房长啥样?
  2. 【晴神宝典刷题路】codeup+pat 题解索引(更新ing
  3. linux FTP云盘
  4. Python数据可视化:香港地图、房价可视化,绘制气泡图
  5. Windows系统禁止软件、驱动阻止系统息屏、睡眠。
  6. 【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)
  7. 微信小程序——富文本
  8. 修复IE不能正常上网的工具
  9. 比我聪明漂亮还比我努力的人,告诉我10个tips
  10. rpm, tar, gz, bz, bz2, rar, zip, lha, deb, 解压