选择多列的max()

时间:2011-12-14 17:06:23

标签: mysql sql

好的,这是我的表:

product_id  version_id  update_id  patch_id
  1           1           0          0
  1           1           1          0
  1           1           1          1
  1           1           2          0
  1           1           2          1
  2           1           0          0
  2           2           0          0
  2           3           0          0
  2           3           0          1
  3           1           0          0
  3           1           0          1

现在我想选择产品的最新版本,因此具有最高update_id和版本的版本PATCH_ID。

例如,

的最新版本
  • 产品1应返回1,2,1
  • 产品2应返回3,0,1
  • 产品3应返回1,0,1

我在使用GROUP BY和HAVING尝试各种各样的东西,尝试了子查询,但我仍然无法找到实现这一目标的方法。

有人可以帮我找到合适的查询,还是应该考虑为此编写一个php函数?

修改

其他一些信息: - 列一起是主键(有更多列,但对于这个问题,它们并不重要) - 没有列是自动增量

这是表格:

CREATE  TABLE IF NOT EXISTS `db`.`patch` (
`product_id` INT NOT NULL ,
`version_id` INT NOT NULL ,
`update_id` INT NOT NULL ,
`patch_id` INT NOT NULL
PRIMARY KEY (`product_id`, `version_id`, `update_id`, `patch_id`) ,
INDEX `fk_patch_update1` (`product_id` ASC, `version_id` ASC, `update_id` ASC) )

修改2

标记为重复,但不是:另一个问题是查找高于三个不同列中任何一列的值的记录。

在这个问题中,我们寻找按product_id分组的最高版本号。

编辑3

rgz的回答再次告诉我,这是重复的。首先:这个问题比较老了。其次,我不认为答案是一样的。

rgz建议使用以下查询:

SELECT product_id, GREATEST(version_id, update_id, patch_id) AS latest_version FROM patch.

GREATEST(1,2,3)返回3,对吗? Wat,如果我们有这些值:

  product_id  version_id  update_id  patch_id
  1           1           0          0
  1           1           2          8
  1           3           0          0

据我了解,此查询将返回:

  product_id  latest_version
  1                 1
  1                 8
  1                 3

但它应该回归:

  product_id  version_id update_id  patch_id
  1           3          0          0

我认为GREATEST无法提供帮助。如果你认为它会,请证明我错了。

7 个答案:

答案 0 :(得分:9)

这是独特标识有用的一个例子。

想象一下,您有一个autoincrememnting ID字段,然后您可以使用相关的子查询找到每个产品所需的ID ...

SELECT
  *
FROM
  yourTable
WHERE
  id = (
        SELECT   id
        FROM     yourTable AS lookup
        WHERE    lookup.product_id = yourTable.product_id
        ORDER BY version_id DESC, update_id DESC, patch_id DESC
        LIMIT    1
       )


没有唯一标识符的等效需要多个相关的子查询...

SELECT
  *
FROM
  yourTable
WHERE
     version_id = (
                   SELECT   MAX(version_id)
                   FROM     yourTable AS lookup
                   WHERE    lookup.product_id = yourTable.product_id
                  )
  AND update_id = (
                   SELECT   MAX(update_id)
                   FROM     yourTable AS lookup
                   WHERE    lookup.product_id = yourTable.product_id
                     AND    lookup.version_id = yourTable.version_id
                  )
  AND patch_id  = (
                   SELECT   MAX(patch_id)
                   FROM     yourTable AS lookup
                   WHERE    lookup.product_id = yourTable.product_id
                     AND    lookup.version_id = yourTable.version_id
                     AND    lookup.update_id  = yourTable.update_id
                  )

这将明显慢于具有唯一标识符列的表。


另一种选择(没有唯一标识符)是在不同的聚合级别上自我加入。

SELECT
  yourTable.*
FROM
  (SELECT product_id, MAX(version_id) AS max_version_id FROM yourTable GROUP BY product_id) AS version
INNER JOIN
  (SELECT product_id, version_id, MAX(update_id) AS max_update_id FROM yourTable GROUP BY product_id, version_id) AS update
    ON  update.product_id = version.product_id
    AND update.version_id = version.max_version_id
INNER JOIN
  (SELECT product_id, version_id, updatE_id, MAX(patch_id) AS max_patch_id FROM yourTable GROUP BY product_id, version_id) AS patch
    ON  patch.product_id = update.product_id
    AND patch.version_id = update.version_id
    AND patch.update_id  = update.max_update_id
INNER JOIN
  yourTable
    ON  yourTable.product_id = patch.product_id
    AND yourTable.version_id = patch.version_id
    AND yourTable.update_id  = patch.update_id
    AND yourTable.patch_id   = patch.max_patch_id

答案 1 :(得分:3)

尝试:

Select product_id, version_id, update_id, patch_id
from (select * from MyTable 
      order by product_id, version_id desc, update_id desc, patch_id desc) as v
group by product_id

对不起,这是@RolandoMySQLDBA。请原谅入侵,但我会根据示例数据发布您的查询及其输出:

mysql> Select product_id, version_id, update_id, patch_id
    -> from (select * from prod
    ->       order by product_id, version_id desc, update_id desc, patch_id desc) as v
    -> group by product_id  ;
+------------+------------+-----------+----------+
| product_id | version_id | update_id | patch_id |
+------------+------------+-----------+----------+
|          1 |          1 |         2 |        1 |
|          2 |          3 |         0 |        1 |
|          3 |          1 |         0 |        1 |
+------------+------------+-----------+----------+
3 rows in set (0.00 sec)

您的查询如图所示。它也比我的回答简单。好节目!!!

答案 2 :(得分:1)

我没有测试它,但我确信你可以做这样的事情

SELECT val.product_id, MAX(val.myVersion)
FROM (SELECT product_id, ((version_id * 10 + update_id) * 10 + patch_id) as myVersion
      FROM myTable) as val
GROUP by product_id;

对不起,这是@RolandoMySQLDBA。请原谅入侵,但我会根据示例数据发布您的查询及其输出:

mysql> SELECT val.product_id, MAX(val.myVersion)
    -> FROM (SELECT product_id, ((version_id * 10 + update_id) * 10 + patch_id) as myVersion
    ->       FROM prod) as val
    -> GROUP by product_id;
+------------+--------------------+
| product_id | MAX(val.myVersion) |
+------------+--------------------+
|          1 |                121 |
|          2 |                301 |
|          3 |                101 |
+------------+--------------------+
3 rows in set (0.01 sec)

虽然显示不同,但您的查询在逻辑上适用于原始问题。来自我的+1!

答案 3 :(得分:0)

您可以按max(update_id * maxpatchidvalue + patch_id)进行分组,以获得您想要的内容。

答案 4 :(得分:0)

试试这个。

SELECT
  version_id,
  max_update.update_id,
  max_patch.patch_id
FROM
  TABLE
    OUTER APPLY
    (
      SELECT TOP 1
        update_id
      FROM
        TABLE t1
      WHERE
        TABLE.version_id = t1.version_id
        AND TABLE.product_id = t1.product_id
      ORDER BY update_id DESC
    ) max_update
    OUTER APPLY
    (
      SELECT TOP 1
        patch_id 
      FROM
        TABLE t1
      WHERE
        TABLE.version_id = t1.version_id
        AND TABLE.product_id = t1.product_id
        AND max_update.update_id = t1.update_id
      ORDER BY patch_id DESC
    ) max_patch

答案 5 :(得分:0)

使用问题中的输入数据和没有键的表格,这是一个相当恶心的方法:

首先,您需要的查询是:

select A.* from prod A LEFT JOIN
(select product_id,max(tagnum) maxtag from
(select *,(version_id*10000+update_id*100+patch_id) tagnum from prod) AA
group by product_id) B
USING (product_id)
WHERE (version_id*10000+update_id*100+patch_id) = B.maxtag;

以下是一些使用所有整数,没有索引和示例数据的示例代码:

DROP TABLE IF EXISTS prod;
CREATE TABLE prod
(
product_id INT,
version_id INT,
update_id INT,
patch_id INT
) ENGINE=MyISAM;
INSERT INTO prod VALUES
( 1,  1,  0, 0 ),
( 1,  1,  1, 0 ),
( 1,  1,  1, 1 ),
( 1,  1,  2, 0 ),
( 1,  1,  2, 1 ),
( 2,  1,  0, 0 ),
( 2,  2,  0, 0 ),
( 2,  3,  0, 0 ),
( 2,  3,  0, 1 ),
( 3,  1,  0, 0 ),
( 3,  1,  0, 1 );
select * from prod;
select *,(version_id*10000+update_id*100+patch_id) tagnum from prod;
select A.* from prod A LEFT JOIN
(select product_id,max(tagnum) maxtag from
(select *,(version_id*10000+update_id*100+patch_id) tagnum from prod) AA
group by product_id) B
USING (product_id)
WHERE (version_id*10000+update_id*100+patch_id) = B.maxtag;

结果如下:

mysql> DROP TABLE IF EXISTS prod;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE prod
    -> (
    -> product_id INT,
    -> version_id INT,
    -> update_id INT,
    -> patch_id INT
    -> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO prod VALUES
    -> ( 1,  1,  0, 0 ),
    -> ( 1,  1,  1, 0 ),
    -> ( 1,  1,  1, 1 ),
    -> ( 1,  1,  2, 0 ),
    -> ( 1,  1,  2, 1 ),
    -> ( 2,  1,  0, 0 ),
    -> ( 2,  2,  0, 0 ),
    -> ( 2,  3,  0, 0 ),
    -> ( 2,  3,  0, 1 ),
    -> ( 3,  1,  0, 0 ),
    -> ( 3,  1,  0, 1 );
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

mysql> select * from prod;
+------------+------------+-----------+----------+
| product_id | version_id | update_id | patch_id |
+------------+------------+-----------+----------+
|          1 |          1 |         0 |        0 |
|          1 |          1 |         1 |        0 |
|          1 |          1 |         1 |        1 |
|          1 |          1 |         2 |        0 |
|          1 |          1 |         2 |        1 |
|          2 |          1 |         0 |        0 |
|          2 |          2 |         0 |        0 |
|          2 |          3 |         0 |        0 |
|          2 |          3 |         0 |        1 |
|          3 |          1 |         0 |        0 |
|          3 |          1 |         0 |        1 |
+------------+------------+-----------+----------+
11 rows in set (0.00 sec)

mysql> select *,(version_id*10000+update_id*100+patch_id) tagnum from prod;
+------------+------------+-----------+----------+--------+
| product_id | version_id | update_id | patch_id | tagnum |
+------------+------------+-----------+----------+--------+
|          1 |          1 |         0 |        0 |  10000 |
|          1 |          1 |         1 |        0 |  10100 |
|          1 |          1 |         1 |        1 |  10101 |
|          1 |          1 |         2 |        0 |  10200 |
|          1 |          1 |         2 |        1 |  10201 |
|          2 |          1 |         0 |        0 |  10000 |
|          2 |          2 |         0 |        0 |  20000 |
|          2 |          3 |         0 |        0 |  30000 |
|          2 |          3 |         0 |        1 |  30001 |
|          3 |          1 |         0 |        0 |  10000 |
|          3 |          1 |         0 |        1 |  10001 |
+------------+------------+-----------+----------+--------+
11 rows in set (0.01 sec)

mysql> select A.* from prod A LEFT JOIN
    -> (select product_id,max(tagnum) maxtag from
    -> (select *,(version_id*10000+update_id*100+patch_id) tagnum from prod) AA
    -> group by product_id) B
    -> USING (product_id)
    -> WHERE (version_id*10000+update_id*100+patch_id) = B.maxtag;
+------------+------------+-----------+----------+
| product_id | version_id | update_id | patch_id |
+------------+------------+-----------+----------+
|          1 |          1 |         2 |        1 |
|          2 |          3 |         0 |        1 |
|          3 |          1 |         0 |        1 |
+------------+------------+-----------+----------+
3 rows in set (0.00 sec)

mysql>

试一试!!!

CAVEAT

我的回答将支持每种产品

  • 最多99个版本
  • 最多99次更新
  • 最多99个补丁

答案 6 :(得分:-1)

MySQL具有GREATEST功能,因此您应该像这样使用它:

SELECT product_id, GREATEST(version_id, update_id, patch_id) AS latest_version FROM patch.

我将这个问题标记为重复,因为这两个问题基本上都是同一个问题,选择几列的最大值。在这里,您可以在SELECT子句中使用它,在WHERE子句中使用它的另一个问题中。在任何一种情况下,GREATEST都可以解决问题。