group_concat()函数总结_MySQL
bitsCN.com group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果。
比较抽象,难以理解。
通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来。要返回哪些列,由函
数参数(就是字段名)决定。分组必须有个标准,就是根据group by指定的列进行分组。
group_concat函数应该是在内部执行了group by语句,这是我的猜测。
1.测试语句:SELECT group_concat(town) FROM `players` group by town
结果去查找town中去查找哪些值是一样的,如果相等,就全部列出来,以逗号分割进行列出,如下:
group_concat(town)
北京,北京
长沙
2.测试:SELECT group_concat( town )
FROM players
结果:
group_concat(town)
长沙,北京,北京,
上面是否可以证明,group_concat只有与group by语句同时使用才能产生效果? 下面进行了实际测验
3.测试常量对group_concat()的配置影响:
SET @@GROUP_CONCAT_MAX_LEN=4
手册中提到设置的语法是这样的:
SET [SESSION | GLOBAL] group_concat_max_len = val;
两种有什么区别?
SET @@global.GROUP_CONCAT_MAX_LEN=4;
global可以省略,那么就变成了:SET @@GROUP_CONCAT_MAX_LEN=4;
4.使用语句 SELECT group_concat(town) FROM `players`。结果得到:
group_concat(town)
长沙,北京,长沙,北京
结论:group_concat()函数需要与group by语句在一起使用,才能得到需要的效果。
原因可以这样理解:group_concat()得到是属于x组的所有成员(函数里面列参数指定需要显示哪些字段)。x组从哪里来?如
果没有group by进行指定,那么根本不知道group_concat()根据哪个分组进行显示出成员。 所以,像上面没有group by子句
的时候,就显示了长沙和北京。
实际中什么时候需要用到这个函数?
假如需要查询的结果是这样:左边显示组名,右边想显示该组别下的所有成员信息。用这个函数,就可以省去很多事情了。
另外,假如我这样使用:SELECT group_concat( name, sex ) FROM `players` town。意义不大。group_concat()指定一个
列是最好的情况。如果指定了多个列。那么显示结果类似这样:
group_concat(name,sex)
王滔,王小明男,刘惠女,舒明女
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

As a scripting language widely used to develop web applications, PHP has very powerful string processing functions. In daily development, we often encounter operations that require deleting a string, especially the last two characters of the string. This article will introduce two PHP techniques for deleting the last two characters of a string and provide specific code examples. Tip 1: Use the substr function The substr function in PHP is used to return a part of a string. We can easily remove characters by specifying the string and starting position
