简体   繁体   中英

MySQL GROUP BY with GROUP_CONCAT return all row merged?

I ran into a problem today. I got a table with coins values :

0.01
0.01
0.01
0.05
0.10
0.25
1.00
1.00
2.00
and so on...

So the main idea is to get all the value one time so 0.01, 0.05, etc..

So I do :

SELECT Valeur FROM Mint_Coins GROUP BY Valeur

Now it will give me one row for each value... I want all values in one row so I use this :

SELECT GROUP_CONCAT(',', Valeur) AS Values FROM Mint_Coins GROUP BY Valeur

It give me again 7 rows in blob... to have blob it mean that the row are over 512 bytes... Okay, let's see when I convert that they contain... results is now :

0.01, 0.01, 0.01, 0.01
0.05, 0.05
etc..

So what I am doing wrong ? I want the result to hold in one colomn and one row like this 0.01,0.05,0.10,0.25,1.00,2.00 .

Thanks

You can pass distinct to group_concat to ignore duplicated values:

SELECT GROUP_CONCAT(DISTINCT Valeur) AS Values FROM Mint_Coins

You also don't need to group by in this case.

If you wanted the results in multiple rows, you could also have done:

SELECT DISTINCT Valeur FROM Mint_Coins
SELECT GROUP_CONCAT(A.Valuer SEPARATOR ',') AS Valuer
FROM (SELECT DISTINCT Valuer FROM Mint_Coins)A ORDER BY Valuer;

Check this valuable link.This will help you.

http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/

你能使用嵌套的子查询吗?

SELECT GROUP_CONCAT(',', (SELECT Valeur FROM Mint_Coins GROUP BY Valeur)) AS Values FROM Mint_Coins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM