码迷,mamicode.com
首页 > 数据库 > 详细

MySQL查询1

时间:2016-07-31 13:05:33      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

1、将下列语句复制到sqlyog的询问栏
技术分享

/*!40101 SET NAMES utf8 */;

create table `t_student` (
`id` double ,
`stuName` varchar (60),
`age` double ,
`sex` varchar (30),
`gradeName` varchar (60)
);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘1‘,‘张三‘,‘23‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘2‘,‘张三丰‘,‘25‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘3‘,‘李四‘,‘23‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘4‘,‘王五‘,‘22‘,‘男‘,‘三年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘5‘,‘珍妮‘,‘21‘,‘女‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘6‘,‘李娜‘,‘26‘,‘女‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘7‘,‘王峰‘,‘20‘,‘男‘,‘三年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘8‘,‘梦娜‘,‘21‘,‘女‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘9‘,‘小黑‘,‘22‘,‘男‘,‘一年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘10‘,‘追风‘,‘25‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘11‘,‘小小张三‘,‘21‘,NULL,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘12‘,‘小张三‘,‘23‘,‘男‘,‘二年级‘);
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values(‘13‘,‘张三锋小‘,‘24‘,NULL,‘二年级‘);

这样就会在选定的数据库db_student里创建一张t_student表。

技术分享

注:查询语句写在询问栏后,全选或者光标在语句中,再点击执行查询(F9)按钮技术分享,就可以在下方的结果栏看到查询结果。

 

2、查询所有字段

技术分享

SELECT id,stuName,age,sex,gradeName FROM t_student;

SELECT * FROM t_student;

技术分享

 

3、查询指定字段

技术分享

 SELECT id,stuName FROM t_student;

技术分享

 

4、Where条件查询

技术分享

SELECT * FROM t_student WHERE id=1;

技术分享

 SELECT * FROM t_student WHERE age>22;

技术分享

 

5、带IN关键字查询

技术分享

 SELECT * FROM t_student WHERE age IN (21,23);

技术分享

SELECT * FROM t_student WHERE age NOT IN (21,23);

技术分享

 

6、带BETWEEN AND的范围查询

技术分享

SELECT * FROM t_student WHERE age BETWEEN 22 AND 24;

技术分享

SELECT * FROM t_student WHERE age NOT BETWEEN 22 AND 24;

技术分享

SELECT * FROM t_student WHERE age NOT BETWEEN 22 AND 24 ORDER BY id DESC;

技术分享

 

7、带LIKE的模糊查询

技术分享

 SELECT * FROM t_student WHERE stuName LIKE ‘珍妮‘;

技术分享

SELECT * FROM t_student WHERE stuName LIKE ‘张三%‘;

技术分享

SELECT * FROM t_student WHERE stuName LIKE ‘张三_‘;

技术分享

SELECT * FROM t_student WHERE stuName LIKE ‘张三__‘;

技术分享

SELECT * FROM t_student WHERE stuName LIKE ‘%张三%‘;

 技术分享

 

8、空值查询

技术分享

SELECT * FROM t_student WHERE sex IS NULL;

技术分享

SELECT * FROM t_student WHERE sex IS NOT NULL;

技术分享

 

9、带AND的多条件查询

技术分享

 SELECT * FROM t_student WHERE gradeName=‘一年级‘ AND age=23;

技术分享

 

10、带OR的多条件查询

 技术分享

SELECT * FROM t_student WHERE gradeName=‘一年级‘ OR age=23;

技术分享

 

11、DISTINCT去重复查询

技术分享

SELECT DISTINCT gradeName FROM t_student;

技术分享

 

12、对查询结果排序

技术分享

 默认升序:

SELECT * FROM t_student ORDER BY age ASC;

技术分享

SELECT * FROM t_student ORDER BY age DESC;

技术分享

 

13、GROUP BY分组查询

技术分享

 SELECT gradeName, GROUP_CONCAT(stuName) FROM t_student GROUP BY gradeName;

技术分享

SELECT gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName;

技术分享

SELECT gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName HAVING COUNT(stuName)>3;

技术分享

SELECT gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName WITH ROLLUP;

技术分享

SELECT gradeName,GROUP_CONCAT(stuName) FROM t_student GROUP BY gradeName WITH ROLLUP;

技术分享

注:group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果

 

14、LIMIT分页查询

技术分享

SELECT * FROM t_student LIMIT 0,5;

技术分享

SELECT * FROM t_student LIMIT 5,5;

技术分享

SELECT * FROM t_student LIMIT 10,5;

技术分享

 

MySQL查询1

标签:

原文地址:http://www.cnblogs.com/stm32stm32/p/5722788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!