0

我有一个名为“定价”的表,其中包含“分销商”和“版本”等多个字段。分配器字段是分配器表中的 id。每个分销商都有不同的版本。像这样的版本:20131109AV-V1,20131110AV-V2,20131112AV-V3是distributor=1 的版本,20131111WC-V1,20131111WC-V2是distributor=2 的版本等。我需要从定价表中选择所有数据,其中版本等于每个分销商的最新版本。

eg: 20131112AV-V3是distributor的最新版本=1 20131111WC-V2是distributor的最新版本=2

我可以一步完成查询吗?

还是我需要逐个查询,首先选择分发者和版本,然后使用分发者和最新版本循环遍历表(代码如下:)?

$all_distributors=mysql_query("SELECT `distributor`,MAX(`version`) as `version` FROM `pricing` GROUP BY `distributor`",$con);
while($all_dist= mysql_fetch_array($all_distributors))
{
    $latest_version=$all_dist['version'];
    $distributor=$all_dist['distributor'];
    echo $distributor."-".$latest_version."<br/>";
        //Again select the query using this $distributor & $latest_version

}

任何想法?

谢谢!

4

1 回答 1

1

您可以为此使用单个查询:

SELECT p.*
FROM pricing p
    JOIN 
        (
            SELECT distributor, MAX(version) AS ver 
            FROM pricing 
            GROUP BY distributor
        ) mx ON mx.ver = p.version AND p.distributor = mx.distributor
于 2013-11-12T01:10:54.390 回答