A problem usually encountered when writing sql, record it
There are three tables student (student table), teacher (teacher table), stu_teacher (student-teacher relationship table)
The table structure and data are as follows
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('1','Zhang San');
INSERT INTO `student` VALUES ('2',' ');
INSERT INTO `student` VALUES ('3',' ');
INSERT INTO `student` VALUES ('4','Zhao Liu');
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `teacher` VALUES ('1','Chinese teacher');
INSERT INTO `teacher` VALUES ('2','mathematics teacher');
INSERT INTO `teacher` VALUES ('3','English teacher');
INSERT INTO `teacher` VALUES ('4','Physical Education Teacher');
INSERT INTO `teacher` VALUES ('5','Music Teacher');
CREATE TABLE `stu_teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stu_id` int(11) DEFAULT NULL,
`teacher_id` int(11) DEFAULT NULL,
`flag` int(11) DEFAULT '0' COMMENT '0-valid 1-invalid',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
INSERT INTO `stu_teacher` VALUES ('1', '1', '1', '0');
INSERT INTO `stu_teacher` VALUES ('2', '1', '2', '0');
INSERT INTO `stu_teacher` VALUES ('3', '1', '3', '0');
INSERT INTO `stu_teacher` VALUES ('4', '2', '2', '0');
INSERT INTO `stu_teacher` VALUES ('5', '2', '3', '0');
INSERT INTO `stu_teacher` VALUES ('6', '2', '4', '0');
INSERT INTO `stu_teacher` VALUES ('7', '2', '5', '0');
INSERT INTO `stu_teacher` VALUES ('8', '3', '1', '0');
INSERT INTO `stu_teacher` VALUES ('9', '3', '2', '0');
INSERT INTO `stu_teacher` VALUES ('10', '3', '3', '0');
INSERT INTO `stu_teacher` VALUES ('11', '3', '4', '0');
INSERT INTO `stu_teacher` VALUES ('12', '3', '5', '0');
INSERT INTO `stu_teacher` VALUES ('13', '4', '1', '1');
Now we need to find out which teachers each student has according to the group of students, one piece of data for each student, and each student can display it
At the beginning sql was
SELECT
s.`name` stuName,
GROUP_CONCAT(t.`name` SEPARATOR ',') as teacherName
FROM
student s
LEFT JOIN stu_teacher st ON s.id = st.stu_id
LEFT JOIN teacher t ON st.teacher_id = t.id
GROUP BY
s.id
The result is as follows

Later, the business requirements only take the valid data of the relational table, and each student must show
Modify sql to
SELECT
s.`name` stuName,
GROUP_CONCAT(t.`name` SEPARATOR ',') as teacherName
FROM
student s
LEFT JOIN stu_teacher st ON s.id = st.stu_id
LEFT JOIN teacher t ON st.teacher_id = t.id
where st.flag = 0
GROUP BY
s.id
The query result is:

As a result, Zhao Liu disappeared. . . . .
Later, I found that the GROUP_CONCAT method can use the case when function, so I modified the sql to
SELECT
s.`name` stuName,
GROUP_CONCAT(
(
CASE st.flag
WHEN 0 THEN
t.`name`
ELSE
NULL
END
) SEPARATOR ','
) AS teacherName
FROM
student s
LEFT JOIN stu_teacher st ON s.id = st.stu_id
LEFT JOIN teacher t ON st.teacher_id = t.id
GROUP BY
s.id
The query results are as follows

Meet demand
The query statement is as follows: Querying the statement found that the data is actually missing as follows: 140020##2014 book-entry interest-bearing (20 issues) national debt##14 interest-bearing na...
One of the requirements in the project is to group the field A and then get the value of the smallest field B in A. I tried subqueries and some other methods that didn't solve the problem very ...
Function: The query column may contain multiple records. At this time, an error will be reported [Err] 1242-Subquery returns more than 1 row, so if you need to merge the data into one record, use this...
table of Contents Two sheets According to a data query table 1 of the data of Table 2 Phase data of the plurality of data query table 1 via Table 2 Data query table 2 by a data of Table 1 Data related...
1. Group_concat function use When we query in SQL, if each user corresponds to multiple data, a user will have multiple lines of data, such as the figure below There is such a function in MySQL to hel...
We know that group by can classify the sql query results according to the group by column. such as: The query results will be displayed in accordance with columnA and columnB. Columns that are not dis...
groub by is grouped by default and not sorted by default. After groub, the first item in the default sort will be selected to return. If the sorted data before grouping is selected and the first sorte...
In most applications,group_concatFunctions are usually used to do rank conversion. in factgroup_concatThe function also has a very important function, which is sorting within groups. The complete synt...
Article catalog 1.1 preface 1.2 simple query 1.2.1 Query all product data 1.2.2 Query the specified field data of the product 1.2.3 Form name and column name 1.2.4 to the weight 1.2.5 Operation in the...