MySQL INNER JOIN select only one row from second table

后端 未结 11 1020
不知归路
不知归路 2020-11-30 21:11

I have a users table and a payments table, for each user, those of which have payments, may have multiple associated payments in the payments

11条回答
  •  感情败类
    2020-11-30 21:36

    Matei Mihai given a simple and efficient solution but it will not work until put a MAX(date) in SELECT part so this query will become:

    SELECT u.*, p.*, max(date)
    FROM payments p
    JOIN users u ON u.id=p.user_id AND u.package = 1
    GROUP BY u.id
    

    And order by will not make any difference in grouping but it can order the final result provided by group by. I tried it and it worked for me.

提交回复
热议问题