foreword
Organize and review the MySQL database, record learning, covering more complete!
MySQL single table query
- foreword
- 1. Single table query
-
- 1.1 Query all fields
- 1.2 Query specified fields
- 1.3 WHERE condition query
- 1.4 Query with IN keyword
- 1.5 Range queries with BETWEEN AND
- 1.6 Fuzzy query with LIKE
- 1.7 Null query
- 1.8 Multi-condition query with AND
- 1.9 Multi-condition query with OR
- 1.10 DISTINCT deduplication query
- 1.11 Sorting query results
- 1.12 GROUP BY group query (emphasis)
- 1.13 LIMIT paging query
- Finish
1. Single table query
Preparation : t_student table, the code is as follows
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,'二年级');
1.1 Query all fields
Grammar :
1. select field 1, field 2, field 3... from table name;
2. select * from table name
Demonstration :
the two statements have the same effect;
select id,stuName,age,sex,gradeName from t_student;
select * from t_student;
1.2 Query specified fields
Grammar :
1. select field 1, field 2, field 3... from table name;
Note : The above query has the same syntax as here, which is to list all the fields in the table, and only need to write out the required fields for the specified fields.
Demo :
select id,stuName from t_student;
1.3 WHERE condition query
Grammar :
1. select field 1, field 2, field 3... from table name where conditional expression;
2. select * from table name where conditional expression;
The first type can query specified fields or all fields with conditions,
the second type can query all fields with conditions
Demo :
select * from t_student where id = 1;
select id,stuName,age from t_student where id = 2;
Note : The syntax for querying specified fields can be used as long as it is a query statement. select field 1, field 2, field 3... from table name , it should be used flexibly, and the following will take querying all as an example (intuitive and convenient)
1.4 Query with IN keyword
Grammar :
1. select * from table name where field in (element 1, element 2, element 3);
2. select * from table name where field not in (element 1, element 2, element 3);
In is followed by a range, within which to query. not in is that the query is not in scope.
Demo :
select * from t_student where id in (1,2,3);
select * from t_student where id not in (1,2,3);
1.5 Range queries with BETWEEN AND
Grammar :
1. select * from table name where field between value 1 and value 2;
2. select * from table name where field not between takes value 1 and takes value 2;
The value 1 is the start value, and the value 2 is the end value
Demo :
select * from t_student where id between 1 and 5;
select * from t_student where id not between 1 and 5;
1.6 Fuzzy query with LIKE
Grammar :
1. select * from table name where field like "string";
2. select * from table name where field like "%string";
3. select * from table name where field like "string%";
4. select * from table name where field like "%string%"
Note :This syntax is usually used as a fuzzy query
5. select * from table name where field like "_string";
6. select * from table name where field like "string_"
7. select * from table name where field like " _ string_ ";
"%" represents any character or multiple characters, and "_" represents a single character. same as above
Demo :
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 "_张三"
select * from t_student where stuName like "张三_"
select * from t_student where stuName like "_张三_"
No data for this condition
1.7 Null query
Grammar :
1. select * from table name where field is null;
1. select * from table name where field is not null;
Demo :
select * from t_student where sex is null;
select * from t_student where sex is not null;
1.8 Multi-condition query with AND
Grammar :
1. select * from table name where conditional expression 1 and conditional expression 2 [and conditional expression n]
Use and to connect multiple conditions, and all the conditions are satisfied.
Demo :
select * from t_student where id >5 and stuName like "%张三%";
1.9 Multi-condition query with OR
Grammar :
1. select * from table name where conditional expression 1 or conditional expression 2 [ ...or conditional expression n ]
Use or to connect multiple conditions, and one condition can be met (I personally think that the use of pure sql is of little significance, and it will be used in the framework, such as the Mybatis-Plus conditional constructor)
Demo :
select * from t_student where id >5 or stuName like "%张三%";
1.10 DISTINCT deduplication query
Grammar :
1. select distinct field name from table name
Demo :
select distinct gradeName from t_student;
1.11 Sorting query results
Grammar :
1. select * from table name order by field name asc [ desc ];
order by sort keyword, asc ascending order, desc descending order, default ascending order
Demo :
select * from t_student order by age asc;
select * from t_student order by age desc;
1.12 GROUP BY group query (emphasis)
Grammar :
1. group by field name [ having conditional expression / with rollup ] ;
Notice:
1. Used alone (meaningless). For example: select * from table name group by field name;
select * from t_student group by age;
It can be seen that although it is grouped according to age, there is only one piece of data for each age, which does not match the actual data (you can go back to the top to view all the data) and does not meet the actual needs, so it is meaningless to use it alone.
2. Used with the GROUP_CONCAT() function.
-- 根据年龄分组,显示每个年龄下的学生姓名
select age,GROUP_CONCAT(stuName) from t_student group by age;
For details on the GROUP_CONCAT() function, please see the detailed explanation of MySQL group_concat() in this article
3. Used with aggregate functions. (The COUNT function counts the number of rows in a table)
-- 根据年龄分组,显示每个年龄下的学生人数。
-- 第一个是同concat函数拼接字符串做显示结果;
-- 第二个直接查人;
select age,CONCAT(COUNT(stuName),"个人") from t_student group by age;
select age,COUNT(stuName) from t_student group by age;
4. Used together with HAVING (limit, filter output results).
-- 根据年龄分组,显示每个年龄下的学生人数(加 HAVING 限制条件,显示学生大于1的数据)。
select age,count(stuName) from t_student group by age having count(stuName)>1;
5. Use with WITH ROLLUP (add a sum row at the end,use less)。
-- 根据年龄分组,显示每个年龄下的学生姓名
-- 最后将全部学生汇总到一行
select age,GROUP_CONCAT(stuName) from t_student group by age with rollup;
1.13 LIMIT paging query
Grammar :
1. select * from table name limit initial position, number of records;
Demo :
select * from t_student limit 0,5;
Paging query is more commonly used, simple and important.
Finish
The above is all the content, welcome to discuss, record learning!