Mysql数据库单表查询
来源:互联网 发布:nginx指定ip访问服务器 编辑:程序博客网 时间:2024/06/04 19:30
(一)单表查询
- 查询所有记录:select*from 表名;
- 查询指定字段记录:select 列名 from 表名 ;
- 查询指定记录:select 字段名1,字段名2…字段名n where 查询条件;
- 使用IN关键字查询
- 使用BETWEEN AND 查询指定范围的记录
- 使用LIKE关键字模糊查询
- 使用AND关键字多 条件查询
- 使用OR关键字多条件查询
- 查询空值用 IS NULL
- 查询结果不重复
- 对查询结果排序(单列排序、多列排序、指定方向排序)
- 分组查询
- 使用LIMIT限制查询结果的数量
(二)演示
CREATE TABLE `person` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(25) DEFAULT NULL, `sex` varchar(45) DEFAULT NULL, `address` varchar(45) DEFAULT NULL, `number` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
1.查询所有记录:select*from 表名;
mysql> select*from person;+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 1 | 凯耐 | 男 | 长沙 | 123 || 2 | 科比 | 男 | 北京 | 234 || 3 | 小丽 | 女 | 武汉 | 345 || 4 | 小五 | 男 | 深圳 | 456 || 5 | 张毅峰 | 男 | 南京 | 567 || 6 | 贺知章 | 男 | 上海 | 789 || 7 | 张一山 | 男 | 北京 | 987 || 8 | 赵丽颖 | 女 | 上海 | 876 || 9 | 天佑 | 男 | | NULL |+----+--------+-----+---------+--------+9 rows in set
2.查询指定字段记录
mysql> select id,name,address from person;+----+--------+---------+| id | name | address |+----+--------+---------+| 1 | 凯耐 | 长沙 || 2 | 科比 | 北京 || 3 | 小丽 | 武汉 || 4 | 小五 | 深圳 || 5 | 张毅峰 | 南京 || 6 | 贺知章 | 上海 || 7 | 张一山 | 北京 || 8 | 赵丽颖 | 上海 |+----+--------+---------+8 rows in set
3.查询满足条件的字段数据
mysql> select id name,address from person where address='上海';+------+---------+| name | address |+------+---------+| 6 | 上海 || 8 | 上海 |+------+---------+2 rows in set
4.使用IN关键字查询满足条件的字段信息
mysql> select id ,name,address from person where address in('上海','北京');+----+--------+---------+| id | name | address |+----+--------+---------+| 2 | 科比 | 北京 || 6 | 贺知章 | 上海 || 7 | 张一山 | 北京 || 8 | 赵丽颖 | 上海 |+----+--------+---------+4 rows in set
5.使用between and 查询指定范围内的数据
mysql> select*from person where id between 5 and 6;+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 5 | 张毅峰 | 男 | 南京 | 567 || 6 | 贺知章 | 男 | 上海 | 789 |+----+--------+-----+---------+--------+2 rows in set
6.使用like关键字模糊查询
mysql> select*from person where name like '张%';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 5 | 张毅峰 | 男 | 南京 | 567 || 7 | 张一山 | 男 | 北京 | 987 |+----+--------+-----+---------+--------+2 rows in setmysql> select*from person where name like '%山';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 7 | 张一山 | 男 | 北京 | 987 |+----+--------+-----+---------+--------+mysql> select*from person where name like '%丽%';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 3 | 小丽 | 女 | 武汉 | 345 || 8 | 赵丽颖 | 女 | 上海 | 876 |+----+--------+-----+---------+--------+2 rows in setmysql> select*from person where name like'小_';+----+------+-----+---------+--------+| id | name | sex | address | number |+----+------+-----+---------+--------+| 3 | 小丽 | 女 | 武汉 | 345 |+----+------+-----+---------+--------+1 row in setmysql> select*from person where name like'_毅_';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 5 | 张毅峰 | 男 | 南京 | 567 |+----+--------+-----+---------+--------+1 row in setmysql> select*from person where name like'__章';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 6 | 贺知章 | 男 | 上海 | 789 |+----+--------+-----+---------+--------+1 row in set
7.AND多条件查询
mysql> select*from person where sex='男'and address='北京';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 2 | 科比 | 男 | 北京 | 234 || 7 | 张一山 | 男 | 北京 | 987 |+----+--------+-----+---------+--------+2 rows in set
8.OR多条件查询
mysql> select*from person where address='上海'or '北京';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 6 | 贺知章 | 男 | 上海 | 789 || 8 | 赵丽颖 | 女 | 上海 | 876 |+----+--------+-----+---------+--------+2 rows in setmysql> select*from person where address='上海'or name='张一山';+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 6 | 贺知章 | 男 | 上海 | 789 || 7 | 张一山 | 男 | 北京 | 987 || 8 | 赵丽颖 | 女 | 上海 | 876 |+----+--------+-----+---------+--------+3 rows in set
9.查询空值用 IS NULL
mysql> select*from person where number is null;+----+------+-----+---------+--------+| id | name | sex | address | number |+----+------+-----+---------+--------+| 9 | 天佑 | 男 | | NULL |+----+------+-----+---------+--------+1 row in set
10.查询结果不重复
mysql> select distinct sex from person where sex='男';+-----+| sex |+-----+| 男 |+-----+1 row in set
11.对查询结果排序(单列排序、多列排序、指定方向排序)
单列升序mysql> select number from person order by number asc;+--------+| number |+--------+| NULL || 123 || 234 || 345 || 456 || 567 || 789 || 876 || 987 |+--------+9 rows in set单列降序mysql> select number from person order by number desc;+--------+| number |+--------+| 987 || 876 || 789 || 567 || 456 || 345 || 234 || 123 || NULL |+--------+9 rows in set
12.分组查询
1.group by的使用mysql> select sex,count(*)from person group by sex;+-----+----------+| sex | count(*) |+-----+----------+| 女 | 2 || 男 | 7 |+-----+----------+2 rows in set2.筛选分组后满足count(sex)>3的组mysql> select sex,count(*)from person group by sex having count(sex)>3;+-----+----------+| sex | count(*) |+-----+----------+| 男 | 7 |+-----+----------+1 row in set3.分组后统计结果mysql> select sex,count(*)from person group by sex with rollup;+------+----------+| sex | count(*) |+------+----------+| 女 | 2 || 男 | 7 || NULL | 9 |+------+----------+3 rows in set
13.使用LIMIT限制查询结果的数量
1.查询前4条记录mysql> select*from person limit 4;+----+-------+-----+---------+--------+| id | name | sex | address | number |+----+-------+-----+---------+--------+| 1 | 凯耐 | 男 | 长沙 | 123 || 2 | 科比 | 男 | 北京 | 234 || 3 | 小丽 | 女 | 武汉 | 345 || 4 | 小五 | 男 | 深圳 | 456 |+----+-------+-----+---------+--------+4 rows in set2.从第5条记录查询,且只查3条记录mysql> select*from person limit 4,3;+----+--------+-----+---------+--------+| id | name | sex | address | number |+----+--------+-----+---------+--------+| 5 | 张毅峰 | 男 | 南京 | 567 || 6 | 贺知章 | 男 | 上海 | 789 || 7 | 张一山 | 男 | 北京 | 987 |+----+--------+-----+---------+--------+3 rows in set
阅读全文
0 0
- Mysql数据库单表查询
- MySQL数据库之单表查询
- mysql数据库常用的查询语句(单表查询)
- MySQL数据库学习05-查询数据:单表查询
- 数据库单表查询
- 数据库----单表查询
- mysql 单表查询
- mysql单表查询
- MySQL 单表查询
- MySQL 单表查询
- MySQL-单表查询
- mysql 单表查询
- MySQL单表查询
- 初学mysql(二)-数据库之表内容单表查询
- mysql数据库的select单表查询语句
- Mysql数据库操作(六)----单表查询练习
- 数据库之单表查询
- 数据库单表的查询
- php正则表达式实例
- 今天在看Head First 设计模式的时候发现了一点问题,可能是翻译的原因吧
- 封装一个适合自己的AutoLayout(基于鸿洋AutoLayout)
- 深入理解BootStrap-- 栅格系统(布局)
- spirng data学习(一)
- Mysql数据库单表查询
- 检测到在集成的托管管道模式下不适用的 ASP.NET 设置
- 技术文章 | 用Python写一个NoSQL数据库
- 前端跨域知识总结
- csdn如何转载别人的文章
- PHP学习笔记--字符串操作 作者:yezi33 字符串的整理: trim():除去字符串开头和末尾的空格或其他字符。函数执行成功时返回删除了string字符串首部和尾部空格的字符串,发生错误时返回空
- centerloss系列文章笔记——人脸检测Face R-CNN
- JAVA初窥:try、catch、finally语句块中的return语句
- 播放器JPlayer的使用及遇到的问题