Home Backend Development PHP Tutorial How to use Mysql's GROUP_CONCAT() function_PHP tutorial

How to use Mysql's GROUP_CONCAT() function_PHP tutorial

Jul 21, 2016 pm 03:53 PM
concat distinct e mysql Instructions function of grammar

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

See all articles