在MySQL中返回每个'group by'的'last'行

时间:2012-02-08 11:46:19

标签: mysql group-by

是否有更有效的方法来执行以下操作?

select * 
    from foo as a
    where a.id = (select max(id) from foo where uid = a.uid group by uid)
    group by uid;
)

这个答案看起来很相似,但这个答案是最好的方法 - How to select the first row for each group in MySQL?

谢谢,

克里斯。

P.S。表格如下:

CREATE TABLE foo (
    id INT(10) NOT NULL AUTO_INCREMENT,
    uid INT(10) NOT NULL,
    value VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `uid` (`uid`)
)

数据:

id, uid, value
 1,   1, hello
 2,   2, cheese
 3,   2, pickle
 4,   1, world

结果:

id, uid, value
 3,   2, pickle
 4,   1, world

有关详细信息,请参阅http://www.barricane.com/2012/02/08/mysql-select-last-matching-row.html

6 个答案:

答案 0 :(得分:33)

尝试此查询 -

SELECT t1.* FROM foo t1
  JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
    ON t1.id = t2.id AND t1.uid = t2.uid;

然后使用EXPLAIN分析查询。


SELECT t1.* FROM foo t1
  LEFT JOIN foo t2
    ON t1.id < t2.id AND t1.uid = t2.uid
WHERE t2.id is NULL;

答案 1 :(得分:4)

使用DELIMITER // CREATE PROCEDURE search_contact_list(IN keyword varchar(255), IN key_country int(10),IN key_status int(10),IN key_religion varchar(100),IN acc_manager int(5),IN acc_position int(10)) BEGIN SELECT contact.*,company.company_name,status.status_name,user.first_name as user_first_name,user.last_name as user_last_name,country.country_name FROM tbl_contact_master as contact LEFT JOIN tbl_status_master as status ON status.status_id=contact.status_id LEFT JOIN tbl_user_master as user ON user.login_id=contact.contact_owner_id LEFT JOIN tbl_company_master as company ON company.company_id=contact.company_id LEFT JOIN tbl_country_master as country ON country.country_id=contact.country WHERE CASE WHEN keyword IS NOT NULL THEN contact.first_name LIKE CONCAT('%', keyword ,'%' ) OR contact.last_name LIKE CONCAT('%', keyword ,'%' ) END AND CASE WHEN key_country IS NOT NULL THEN contact.country = key_country ELSE NULL END; END // DELIMITER ; 子句返回MySQL中每个GROUP BY的最后一行:

WHERE

答案 2 :(得分:2)

如果表格大小。使view包含所有最后一行ID

create view lastrecords as (select max(id) from foo where uid = a.uid group by uid)

现在使用此view加入您的主查询。它会更快。

  SELECT t1.* FROM tablename as t1
    JOIN lastrecords as  t2
    ON t1.id = t2.id AND t1.uid = t2.uid;

或者你也可以直接在查询中加入最后的记录:

SELECT t1.* FROM tablename as t1
JOIN (SELECT uid, MAX(id) id FROM tablename GROUP BY id) as  t2
ON t1.id = t2.id AND t1.uid = t2.uid;

答案 3 :(得分:1)

最简单的方法:您可以从已排序的选定列表中选择

SELECT * FROM (SELECT * FROM foo order by id DESC) AS footbl
group by uid
order by id DESC

答案 4 :(得分:0)

这对我有用,谢谢!

SELECT t1.* FROM foo t1
  JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
    ON t1.id = t2.id AND t1.uid = t2.uid;

我的版本:

SELECT * FROM messages t1 JOIN (SELECT MAX(id) id FROM messages  where uid = 8279 and actv=1 GROUP BY uid ) t2 ON t1.id = t2.id ORDER BY datex desc LIMIT 0,10;

下面这段代码不会返回我想要的所有行,因为如果max id列是非活动列,那么即使该组有活动行,它也会跳过该组..

select * from messages where uid = 8279 and actv=1 and id In (Select max(id) From messages Group By uid) order by datex desc;

答案 5 :(得分:-1)

此代码对我有效:

public void onAddProductAddPart(ActionEvent event) {

// this is triggered when the Add button is clicked

Part selectedItem = addProductTableViewAll.getSelectionModel().getSelectedItem();
selectedItem.addAssociatedPart(); // addAssociatedPart() is method of Product, not Part
Product.selectedItem.addAssociatedPart(); // Product class has no static member selectedItem   
selectedItem.Product.addAssociatedPart(); // syntax error
addAssociatedPart(selectedItem); // addAssociatedPart() is not method of AddProcutController
Product.addAssociatedPart(selectedItem); // if you reference the method start with a class, the method is expected to be a static method. addAssociatedPart() is not a static method, call it with a product instance

InHouse newPart = new InHouse(1, "test", 1.99, 1, 1, 1, 101);
addAssociatedPart(newPart); // addAssociatedPart() is not part of AddProductController
Product.addAssociatedPart(newPart); // dont reference non-static method with a class name
newPart.addAssociatedPart(); // addAssociatedPart() is not part of Part


addProductTableViewPartial.setItems(associatedParts);
}