第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL GROUP_CONCAT函數(shù)

標(biāo)簽:
MySQL

Summary: in this tutorial, you will learn how to use the MySQL GROUP_CONCAT function to concatenate strings from a group of values with various options.

Introduction to MySQL GROUP_CONCAT function

The GROUP_CONCAT function concatenates strings from a group into one string with various options.

The following illustrates the GROUP_CONCAT function:

GROUP_CONCAT(DISTINCT expression ORDER BY {column_name | usinged_integer | expression} SEPARATOR sep);

Let’s examine the syntax of the GROUP_CONCAT function in more detail:

  • You specify the DISTINCT clause to eliminate duplicate values in a group before combining values.

  • The ORDER BY clause allows you to sort the values in ascending or descending order before concatenating values. The ORDER BY clause sort the values in ascending ( ASC) order by default. If you want to sort the values in the descending order, you need to specify the DESC explicitly.

  • The SEPARATOR specifies a literal value inserted between values in the group. If you do not specify a separator, the GROUP_CONCAT function use a comma (,) as the default separator.

  • The GROUP_CONCAT function ignores NULL values. It returns NULL if there was no matching row found or there were no non-NULL values. The GROUP_CONCAT function returns a binary or non-binary string, which depends on the arguments. The maximum length of the return string is 1024 by default. You can extend the returned value’s length by setting the group_concat_max_lensystem variable at SESSION or GLOBAL level.

MySQL GROUP_CONCAT Examples

Let’s take a look at the customers table in the sample database.

customers table

To get all countries where customers locate as a comma-separated string, you use the GROUP_CONCAT function as follows:

SELECT GROUP_CONCAT(country) FROM customers;

MySQL GROUP_CONCAT example

However, some customers locate in the same country. To remove the duplicate country’s names, you add the DISTINCT clause as the following query:

SELECT GROUP_CONCAT(DISTINCT country) FROM customers;

MySQL GROUP_CONCAT DISTINCT

It is more readable if the country’s names are in ascending order. To sort the country’s name before concatenating, you use the ORDER BY clause as follows:

SELECT GROUP_CONCAT(DISTINCT country ORDER BY country) FROM customers;

MySQL GROUP_CONCAT ORDER BY

To change the default separator of the returned string from a comma (,) to a semi-colon (;), you use the SEPARATORclause as the following query:

SELECT GROUP_CONCAT(DISTINCT country ORDER BY country SEPARATOR ';') FROM customers;

MySQL GROUP_CONCAT SEPARATOR

MySQL GROUP_CONCAT with CONCAT_WS function example

The CONCAT_WS concatenates strings with a specified separator. You can combine the GROUP_CONCAT function with the CONCAT_WS function to make a useful result.

For example, you can concatenate the last name and first name of each customer’s contact by using the CONCAT_WSfunction. The result is the contact’s full name. Then, you create a semicolon-separated string that contains all contact’s full names as the following query:

SELECT GROUP_CONCAT( CONCAT_WS(', ',contactLastName,contactFirstName) SEPARATOR ';') FROM customers;

Notice that GROUP_CONCAT function concatenates values from different rows, while the CONCAT_WS or CONCAT function joins two or more values.

MySQL GROUP_CONCAT function common mistakes

There are some common mistakes when using the GROUP_CONCAT function.

The GROUP_CONCAT function returns a single string. It means you cannot use it with IN operator e.g., within a subquery.

For example, the GROUP_CONCAT function returns the result of 12, and 3 as '1, 2, 3' string. If you use this result with the IN operator, it would be IN ('1,2,3'), while the correct one must be IN (1,2,3), therefore the query may not return any result. For example, the following query will not work as desired.

SELECT id, name FROM table_name WHERE id IN GROUP_CONCAT(id) ;

The GROUP_CONCAT function is an aggregate function. To sort the values, you must use the ORDER BY clause inside the function, not in the ORDER BY after the FROM or WHERE clause in the SELECT statement. The following example demonstrates the incorrect usage of the ORDER BY clause in the context of using the GROUP_CONCAT function:

SELECT GROUP_CONCAT(DISTINCT country  SEPARATOR ';') FROM customers ORDER BY country;

The SELECT clause returns one string value so the ORDER BY clause does not take any effect in this statement.

MySQL GROUP_CONCAT applications

There are many cases where you can apply the GROUP_CONCAT function to produce useful results. The following list is some common examples of the using the GROUP_CONCAT function.

  • Use GROUP_CONCAT function to make a comma-separated user’s roles such as ‘admin,author,editor’.

  • Use GROUP_CONCAT function to return the comma-separated user’s hobbies e.g., ‘design,programming,reading’.

  • Use GROUP_CONCAT function to build a tag for blog posts, articles or products e.g., ‘mysql, mysql aggregate function, mysql tutorial’.

In this tutorial, we have introduced you to MySQL GROUP_CONCAT function that concatenates non- NULL values from a group of strings into a string with various options.

原文链接:http://outofmemory.cn/mysql/function/mysql-group_concat

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消