SQL选择行,其中id = max(id)

时间:2011-10-18 15:06:58

标签: mysql sql aggregate-functions greatest-n-per-group

我有一个表格,其中包含产品信息,其中包含具有相同标题的产品的多个实例,由不同颜色及其ID标识。我需要输出整个行,其中id =按标题分组的最大ID,但我似乎无法做到这一点。这是一个非常简化的表和一些示例数据:

id   title   colour   description

1    rico    red      blah1
2    rico    blue     blah2
3    rico    yellow   blah3
4    katia   black    blah4
5    katia   white    blah5

在我的代码示例中,当我想要3 rico yellow blah3时,我得到1 rico red blah1。

以下是我正在使用的代码:

SELECT pd_id, pd_title, pd_description, pd_colour,
       pd_price,pd_large_image,pd_date,cat_sub_id_3,pd_new
FROM product 
WHERE 
  cat_sub_id_1 = '".$cat_sub_id."' 
  AND cat_parent_id='".$cat_parent_id."' 
GROUP BY pd_title 
HAVING MAX(pd_id) 
ORDER BY pd_id DESC

更新:谢谢大家,

我使用了alinoz的答案来提出以下代码:)

SELECT
    pd_id,pd_title,pd_description,pd_colour,pd_price,pd_large_image,pd_date,cat_sub_id_3,pd_new
FROM product 
HERE cat_sub_id_1 = '".$cat_sub_id."' AND cat_parent_id='".$cat_parent_id."'
AND pd_id IN (
    SELECT max(pd_id)
    FROM product
    WHERE cat_sub_id_1 = '".$cat_sub_id."' AND cat_parent_id='".$cat_parent_id."'
    GROUP BY pd_title
    )
GROUP BY pd_title
ORDER BY pd_id DESC

7 个答案:

答案 0 :(得分:19)

Aaahhh,好老 ......

select *
from YourTable yt
inner join(
    select title, max(id) id
    from YourTable
    group by title
) ss on yt.id = ss.id and yt.title = ss.title

当然,你应该根据你的需要调整它。

另外,我认为这是“必读”:SQL Select only rows with Max Value on a Column

答案 1 :(得分:8)

我认为这可能有效(未经测试):

SELECT * FROM that_table WHERE id IN (
SELECT MAX(id) FROM that_table GROUP BY title
) /* AND this = that */

答案 2 :(得分:5)

只需一行(在本例中为'id')最大值

SELECT * FROM `your_table` order by id desc limit 1

答案 3 :(得分:3)

尝试使用pd_id =添加一个子句到你的位置(从...中选择max(pd_id))

SELECT pd_id, pd_title, pd_description, pd_colour,
       pd_price,pd_large_image,pd_date,cat_sub_id_3,pd_new
FROM product 
WHERE 
  cat_sub_id_1 = '".$cat_sub_id."' 
  AND cat_parent_id='".$cat_parent_id."' 
  AND pd_id = (select max(pd_id) from product)
GROUP BY pd_title

此查询不是最佳的,但它可以完成这项工作。

答案 4 :(得分:2)

除了阿德里安和萨尔曼的方法之外,还有另外一个,当有关系,两个或更多行具有相同(最大)id时,它给出不同的结果。它将显示每个标题只有一行,而另一个查询将显示所有这些(在您的情况下,您可能无关,因为您按ID排序,我认为是主键):

SELECT 
    t.* 
FROM 
    TableX AS t
  JOIN
    ( SELECT DISTINCT
          title                        --- what you want to Group By
      FROM TableX 
    ) AS dt
    ON t.PK =                          --- the Primary Key of the table
       ( SELECT tt.PK
         FROM TableX AS tt
         WHERE tt.title = dt.title
         ORDER BY id ASC               --- (or DESC) what you want to Order By
         LIMIT 1
       )

答案 5 :(得分:0)

因为您已经订购,所以您可以在where条件中使用行号= 1。见link。因为你的数据库不知道。我无法提供具体的查询,但指导你的方向

或尝试sql top将产生相同的结果,因为使用了order by

答案 6 :(得分:0)

我认为,没有必要使用子选择:

select 
SUBSTRING_INDEX(group_concat(testtab.ActionID order by testtab.ActionID DESC SEPARATOR '|'), '|', 1)  as ActionIDs,
SUBSTRING_INDEX(group_concat(testtab.DiscountedPrice order by testtab.ActionID DESC SEPARATOR '|'), '|', 1)  as DiscountedPrices
testtab.*
from testtab 
where 
testtab.StartDate <= 20160120 
and testtab.EndDate > 20160120
and testtab.VariantID = 302304364
and testtab.DeleteStatus = 0
group by testtab.VariantID
order by testtab.VariantID, testtab.ActionID;

它只是我测试中的一个快速示例。只需通过shure在substring_index中以相同的方式订购并使用一个好的分隔符。

玩得开心

相关问题