MySQL column to row conversion, method of merging fields (must read)

MySQL column to row conversion, method of merging fields (must read)

Data Sheet:

Column to row: using max(case when then)

max---aggregate function takes the maximum value

(case course when 'Chinese' then score else 0 end) ---Judgement

as language---alias as column name

SELECT
 `name`,
 MAX(
  CASE 
  WHEN course = 'Chinese' THEN
   score
  END
 ) AS Language,
 MAX(
  CASE 
  WHEN course = 'Mathematics' THEN
   score
  END
 ) AS Mathematics, 
 MAX(
  CASE 
  WHEN course = 'English' THEN
   score
  END
 ) AS English FROM
 student
GROUP BY `name`
; 

Merge field display: Use group_cancat(course,":","score")

SELECT
 `name`,
 GROUP_CONCAT(course, ":", score) AS score FROM
 student
GROUP BY
 `name`; 

group_concat(), the manual states: This function returns a string result with non-NULL values ​​from a group connection.
It is relatively abstract and difficult to understand.

To put it simply, group_concat() will calculate which rows belong to the same group and display the columns that belong to the same group. Which columns to return is determined by the function

The number of parameters (that is, the field name) determines. There must be a standard for grouping, that is, grouping according to the column specified by group by.

My guess is that the group_concat function should execute the group by statement internally.

1. Test statement:

SELECT
 GROUP_CONCAT(`name`)
FROM
 student
GROUP BY
 `name`;

The result is to find which values ​​in name are the same. If they are equal, list them all, separated by commas, as follows:

group_concat('name')

2. Test:

SELECT
 GROUP_CONCAT(`name`)
FROM
 student
;

result:

group_concat('name')

Can it be proved that group_concat can only be effective when used with a group by statement? The following is a practical test

3. Test the effect of constants on the configuration of group_concat():

SET @@GROUP_CONCAT_MAX_LEN=4

The manual states that the syntax for the setting is:

SET [SESSION | GLOBAL] group_concat_max_len = val;

What is the difference between the two?

SET @@global.GROUP_CONCAT_MAX_LEN=4;
global can be omitted, then it becomes: SET @@GROUP_CONCAT_MAX_LEN=4;

4. Use statements

SELECT
GROUP_CONCAT(`name`)
FROM
student;

The result is:

group_concat('name')

Conclusion: The group_concat() function needs to be used together with the group by statement to achieve the desired effect.

The reason can be understood as follows: group_concat() obtains all members belonging to group x (the column parameters in the function specify which fields need to be displayed). Where does group x come from?

If no group by is specified, then it is not known which group group_concat() will use to display the members. So, when there is no group by clause above, it shows Liu Bei, Guan Yu, Zhang Fei, Liu Bei, Guan Yu, Zhang Fei, Liu Bei, Guan Yu, Zhang Fei.

When is this function actually needed?

Suppose the query result is as follows: the group name is displayed on the left, and all member information under the group is displayed on the right. Using this function can save you a lot of trouble.

Also, if I use:

SELECT
 `name`,
 GROUP_CONCAT(course, ":", score) AS score FROM
 student
;

It doesn't make much sense.

Specifying a single column is the best approach for group_concat(). If multiple columns are specified.

SELECT
 `name`,
 GROUP_CONCAT(course, ":", score) AS score FROM
 student
GROUP BY
 `name`;

Then the displayed result is similar to this:

group_concat(course,":",score)

The above article on how to convert columns to rows and merge fields in MySQL (must read) is all I want to share with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to split and merge multiple values ​​in a single field in MySQL
  • Implementation steps of Mysql merge results and horizontal splicing fields
  • Analysis of MySQL Union merge query data and table alias and field alias usage
  • Group character merge SQL statement merges one of the strings by a certain field (simple merge)
  • An example of merging a field value in SQL Server
  • How to merge two fields in MySQL
  • SQL function to merge a field together

<<:  A brief analysis of how to change the root password in Linux suse11 if you forget it

>>:  Linux server quick uninstall and install node environment (easy to get started)

Recommend

Six methods for nginx optimization

1. Optimize Nginx concurrency [root@proxy ~]# ab ...

Docker time zone issue and data migration issue

Latest solution: -v /usr/share/zoneinfo/Asia/Shan...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

Windows Server 2016 Standard Key activation key serial number

I would like to share the Windows Server 2016 act...

Example of Vue implementing fixed bottom component

Table of contents 【Effect】 【Implementation method...

How to add fields and comments to a table in sql

1. Add fields: alter table table name ADD field n...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

Linux sftp command usage

Concept of SFTP sftp is the abbreviation of Secur...

What to do if you forget the initial password of MySQL on MAC

The method to solve the problem of forgetting the...

Share the problem of Ubuntu 19 not being able to install docker source

According to major websites and personal habits, ...

Linux hardware configuration command example

Hardware View Commands system # uname -a # View k...

How to reference jQuery in a web page

It can be referenced through CDN (Content Delivery...

A brief discussion on CSS height collapse problem

Performance For example: HTML: <div class=&quo...