如何再次将我的SQL结果分组 [英] How do I group by again my SQL result

查看:261
本文介绍了如何再次将我的SQL结果分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我想知道如何将其他列的sql结果分组:



Hey,
I wonder how can i group this result of sql by other column:

SELECT * FROM `feed` where `sku` = '12356' and `id` IN ( SELECT MAX(id) FROM `feed` where `sku` = '12356' GROUP BY `coupon` ) ORDER BY `feed`.`id` DESC LIMIT 5



这个sql查询给了我一些结果,我想用info列对这个结果进行分组。当我尝试按信息编写分组时,它不会从此结果中分组...不确定我该怎么做...



我从这个SQL查询得到的结果:



ID | Sku |优惠券|信息



22 12356 abcf yes9

19 12356 abc yes9

16 12356 gsf no0
$ b $ 12 12356 sds yes2

9 12356 sds yes2



我想要的是通过info得到这个结果组,就像这个:



ID | Sku |优惠券|信息



22 12356 abcf yes9

16 12356 gsf no0

12 12356 sds yes2



希望很清楚..

谢谢



我尝试了什么:



i尝试了我所知道并在google上找到的所有内容


This sql query gives me some results and i want to group this result with "info" column. When i try to write group by info it not group from this results... Not sure how can i do it..

What i get from this sql query:

ID | Sku | Coupon | Info

22 12356 abcf yes9
19 12356 abc yes9
16 12356 gsf no0
12 12356 sds yes2
9 12356 sds yes2

What i want is to get this result group by "info", like this:

ID | Sku | Coupon | Info

22 12356 abcf yes9
16 12356 gsf no0
12 12356 sds yes2

Hope it's clear..
Thanks

What I have tried:

i tried everything i know and found on google

推荐答案

问题在于分组信息不返回单行或可以聚合的值:

The problem is that grouping by Info doesn't return a single row, or a value that can be aggregated:
yes9    abcf, abc
noO     gsf
yes2    sds, sds

那么SQL应该选择哪个值?它不知道你想要什么,所以你需要做一些更复杂的事情。



So which value should SQL select? It doesn't "know" what you want, so you need to do something a little more complex.

SELECT x.ID, Sku, Coupon, Info FROM MyTable m
JOIN (SELECT MAX(ID) AS ID FROM MyTable GROUP BY Info ) x ON x.ID = m.ID

会做什么它与您提供的数据,但它会在现实世界中工作吗?取决于您的数据......

Will do it with the data you provide, but will it work in the "real world"? Depends on your data...


这篇关于如何再次将我的SQL结果分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆