且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 PHP 表中选择行的子集

更新时间:2022-12-03 09:36:04

您可以有一个子查询,分别获取每个卖家的最大金额,然后再次将其与表连接以获取额外的列.

You can have a subquery which separately get the greatest amount for every seller and join it with the table again to get the extra columns.

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT  seller, MAX(amount) amount
            FROM    tableName
            GROUP   BY seller
        ) b ON a.seller = b.seller AND
                a.amount = b.amount

SELECT  a.*
FROM    tableName a
WHERE   a.amount =
        (
            SELECT  MAX(amount)
            FROM    tableName b
            WHERE   a.seller = b.seller
        )

╔════╦════════╦═══════╦════════╗
║ ID ║ SELLER ║ PRICE ║ AMOUNT ║
╠════╬════════╬═══════╬════════╣
║  3 ║ tom    ║   400 ║    750 ║
║  4 ║ jerry  ║   700 ║    250 ║
╚════╩════════╩═══════╩════════╝