How to use Mysql's GROUP_CONCAT() function_PHP tutorial

WBOY
Release: 2016-07-21 15:53:14
Original
1301 people have browsed it

Syntax:

GROUP_CONCAT([DISTINCT] expr [,expr ...][ORDER BY {unsigned_integer | col_name | expr}[ASC | DESC] [,col_name ...]][SEPARATOR str_val])

Let’s demonstrate this function. First, create a student course selection table student_courses and fill in some test data.

SQL code

Copy code The code is as follows:

CREATE TABLE student_courses ( 
 student_id INT UNSIGNED NOT NULL,
courses_id INT UNSIGNED NOT NULL,
KEY(student_id)
);
INSERT INTO student_courses VALUES (1, 1), (1, 2), (2, 3), ( 2, 4), (2, 5);


To find the course selected by student ID 2, use the following SQL:

SQL code
Copy code The code is as follows:

mysql> SELECT student_id, courses_id FROM student_courses WHERE student_id=2;
+- -----------+----------------+ 
| student_id | courses_id | 
+------------+ ------------+                                                                              
+--------- ---+----------------+
3 rows IN SET (0.00 sec)



The output result has 3 records, indicating the student ID Three courses, 3, 4, and 5, were selected for student 2.
If placed in PHP, a loop must be used to get these 3 records, as shown below:



PHP code



Copy the code
The code is as follows: foreach ($pdo->query("SELECT student_id, courses_id FROM student_courses WHERE student_id=2") as $row) { 
$result[] = $row['courses_id'];
}


If you use the GROUP_CONCAT() function and the GROUP BY statement, it is very simple, as shown below:


SQL code



Copy code
The code is as follows: mysql> SELECT student_id, GROUP_CONCAT( courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id; 🎜>+------------+---------+                                                                    ----+---------+
1 row IN SET (0.00 sec)



This way the processing in php is simple:



PHP code


Copy code

The code is as follows:


$row = $pdo-> query("SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id");
$result = explode(',', $row['courses']); The

separator can also be customized. The default is "," as the separator. If you want to change it to "|||", use SEPARATOR to specify it, for example:



SQL code
Copy code The code is as follows:

SELECT student_id, GROUP_CONCAT(courses_id SEPARATOR '|||') AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;

In addition, you can also sort the values ​​of this group and then concatenate them into strings, for example, sort by courses_id in descending order:

SQL code
Copy code The code is as follows:

SELECT student_id, GROUP_CONCAT(courses_id ORDER BY courses_id DESC) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318754.htmlTechArticleSyntax: GROUP_CONCAT([DISTINCT]expr[,expr...][ORDERBY{unsigned_integer|col_name|expr} [ASC|DESC][,col_name...]][SEPARATORstr_val]) Let’s demonstrate this function. First, create a learning...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!