mysql group query GROUP_CONCAT filter data

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

Intelligent Recommendation

mysql query group_concat() incomplete data leads to data truncation problem

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...

Mysql grouping syntax group by get data skills ---- group_concat

  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 ...

The function group_concat in mysql (old fields of multiple data) as new fields ……GROUP BY

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...

MySQL comma format related data query Regexp + Replace + Group_Concat

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...

More Recommendation

MySQL function group_concat multi -line query data merger

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...

MySQL in GROUP BY combined with the use of GROUP_CONCAT

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...

Guaranteed order of group by GROUP_CONCAT of Mysql

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...

mysql group inner row-group_concat

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...

Mysql simple query, filter query, group query, pagination query

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top