[英]How to get max value from group by and groupconcat other field in mysql?
I tried to get the max value corresponding row and group_concat all the email address.我试图获取所有 email 地址的最大值对应的行和 group_concat。
MySql table: MySql表:
id firstName LastName email value
1 John Seam sa@gmail.com 450
2 John Seam js@yahoo.com 0
3 Richard Min aa@gmail.com 0
expected output:预期 output:
id firstName LastName email value
1 John Seam sa@gmail.com|js@yahoo.com 450
3 Richard Min aa@gmail.com 0
I tried the following query:我尝试了以下查询:
select id,firstName,LastName,group_concat(email) ,max(value)
from table
group by firstName,LastName
but it gave wrong result:但它给出了错误的结果:
id firstName LastName email value
2 John Seam sa@gmail.com|js@yahoo.com 450
3 Richard Min aa@gmail.com 0
instead of id 1 I am getting id 2. If I remove the group_concat it gives the correct output.而不是 id 1 我得到 id 2。如果我删除 group_concat 它会给出正确的 output。
If you want the id
of the row with the max value
then use GROUP_CONCAT()
for the ids ordered by value DESC
and take the first one of the result with the function SUBSTRING_INDEX()
:如果您想要具有value
的行的id
,则将GROUP_CONCAT()
用于按value DESC
排序的 id,并使用 function SUBSTRING_INDEX()
获取结果的第一个:
SELECT SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY value DESC), ',', 1) id,
firstName, LastName,
GROUP_CONCAT(email SEPARATOR '|') email,
MAX(value) value
FROM tablename
GROUP BY firstName, LastName
See the demo .请参阅演示。
Results:结果:
id ID | firstName名 | LastName姓 | mail邮件 | value价值 |
---|---|---|---|---|
1 1 | John约翰 | Seam接缝 | sa@gmail.com|js@yahoo.com sa@gmail.com|js@yahoo.com | 450 450 |
3 3 | Richard理查德 | Min敏 | aa@gmail.com aa@gmail.com | 0 0 |
This answers the original version of the question.这回答了问题的原始版本。
Your query is malformed -- the select
is inconsistent with the group by
.您的查询格式不正确 - select
与group by
不一致。 I would suggest a very different approach:我会建议一种非常不同的方法:
select t.*,
(select group_concat(t2.email)
from table t2
) as emails
from table t
order by value desc
limit 1;
select id,firstName,LastName,group_concat(email),max(value) from table group by firstName,LastName select id,firstName,LastName,group_concat(email),max(value) from table group by firstName,LastName
You can use something like this, if you have more than one row with 450, you will need to add also a rownumber to it你可以使用这样的东西,如果你有不止一行 450,你还需要添加一个行号
CREATE TABLE Table1 (`id` int, `firstName` varchar(7), `LastName` varchar(4), `email` varchar(12), `value` int); INSERT INTO Table1 (`id`, `firstName`, `LastName`, `email`, `value`) VALUES (1, 'John', 'Seam', 'sa@gmail.com', 450), (2, 'John', 'Seam', 'js@yahoo.com', 0), (3, 'Richard', 'Min', 'aa@gmail.com', 0);
SELECT t1.`id`, t1.`firstName`, t1.`LastName`,mailG,max_value FROM Table1 t1 INNER JOIN (SELECT `firstName`, `LastName`,GROUP_CONCAT(`email` SEPARATOR ' | ') mailG, MAX(`value`) max_value FROM Table1 GROUP BY `firstName`, `LastName`) t2 ON t1.`firstName` = t2.`firstName` AND t1.`LastName` = t2.`LastName` AND t1.`value` = t2.max_value
id |编号 | firstName |名字 | LastName |姓氏 | mailG |邮件G | max_value -: |:-------- |:------- |:-------------------------- |最大值 -: |:-------- |:-------- |:-------------------------- | --------: 1 | --------: 1 | John |约翰 | Seam |接缝 | sa@gmail.com | sa@gmail.com | js@yahoo.com | js@yahoo.com | 450 3 | 450 3 | Richard |理查德 | Min |最小 | aa@gmail.com | aa@gmail.com | 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.