Use the MySQL function to group_concat a little bit of attention

Source: Internet
Author: User

This function is a very useful function, for example, we can use in many-to-many association tables for batch queries.

For example, students and teachers are many-to-many relationships, if we want to query the specific teachers of some of the students have what, the general situation, we may traverse teacher_id, to get the corresponding student collection, so that the efficiency is very poor, but with the Group_concat function , you can use the following methods to get

Select Teacher_id,group_concat (student_id) from teache_student_mapping where teacher_id in (Teacher_id1,teacher_id2, TEACHER_ID3) GROUP BY teacher_id

In this way, each teacher's corresponding student collection is acquired.

For example, in the use of Lucene and other full-text search search engine is not support table association, so to store this association, one way is to connect the data with some specific characters, and our data source is often a database, this time, Group_concat again come in handy, Usage or the same as above, I'm not going to say much.

But here is a hidden problem, that is, the string composed of Group_concat has a length limit, has not known this problem, the specific can be

Show VARIABLES like ' Group_concat_max_len ' to query

If you want to modify this information, you can

SET Group_concat_max_len = 20000

Modify current session This parameter value

or modify the configuration file

Group_concat_max_len = 102400

Make permanent settings

After changing the configuration file, remember to restart the database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.