下面是我的桌子
我想要的结果如下:
发布于 2012-10-05 22:38:14
有点像戈登·林诺夫的答案。在sql server中,您不使用限制。
[法][法]
select utss.attributeid, utss.attributeValue
from user_trans_service_selection utss
inner join
(select top 3 attributeid, max(uniqueid) as maxid
from user_trans_service_selection
group by attributeid
order by max(uniqueid)
) attr
on attr.maxid = utss.uniqueid发布于 2012-10-05 21:47:33
您需要的是对每个属性具有最高id的行。您需要做一个连接才能得到以下内容:
select utss.attributeid, utss.attributeValue
from user_trans_service_selection utss join
(select attributeid, max(uniqueid) as maxid
from user_trans_service_selection
group by attributeid
) attr
on attr.maxid = utss.id
order by maxid desc
limit 0, 3发布于 2012-10-05 22:39:46
//我们将按函数使用组
从column_name,aggregate_function( column_name )从table_name中按column_name选择column_name操作符值组
//按照以下V步骤{A、B和C}为每个attributeId查找最后添加的记录// {A}对每个attributeId查找最后添加的记录(例如,最高uniqueId)
按attributeId,max(uniqueId)从table_name组中按attributeId选择
// {B}我们将在最后查询的WHER-子句中嵌套这个结果,但首先我们必须去掉attributeId
选择uniqueId (按attributeId从table_name组中选择attributeId,max(uniqueId) )
// {C}现在我们可以创建最终的查询,并从表table_name中选择我们需要的内容。在这个查询的WHER-子句中,我们可以设置一个过滤器,只显示我们在{B}中找到的id所需的信息。
从uniqueId,attributeId,attributeValue中选择attributeId IN (从table_name组中选择attributeId (从attributeId选择attributeId,max(uniqueId)
https://stackoverflow.com/questions/12754448
复制相似问题