简体   繁体   English

MySQL-在嵌套语句中选择最高修订

[英]MySQL - Select max revision within nested statement

I would like to select max revision drawings from mysql table. 我想从mysql表中选择最大修订版图纸。 I can't use anything but nested select statement(?) (all conditions should be after 'SELECT * FROM drawings '). 除了嵌套的select语句(?)外,我什么也不能使用(所有条件应在“ SELECT * FROM drawings ”之后)。

So, the 'drawings' table is: 因此,“图纸”表为:

+----+---------+-------------+-------+---------+---------------------+
| id | number  | title       |format | revision| date                |
+----+---------+-------------+-------+---------+---------------------+
| 100|  022588 |  some title | dwg   | 1       | 2016-01-07 08:00:00 |
| 101|  022588 |  some title | dwg   | 2       | 2016-01-07 08:01:00 |
| 103|  022588 |  some title | pdf   | 3       | 2016-01-07 08:15:32 |
| 104|  022588 |  some title | dwg   | 3       | 2016-01-07 09:10:32 |
+----+---------+-------------+-------+---------+---------------------+

Result I would like to get is (same number, largest revision for appropriate format): 我想得到的结果是(相同编号,相应格式的最大修订版本):

| 103|  022588 |  some title | pdf   | 3       | 2016-01-07 08:15:32 |
| 104|  022588 |  some title | dwg   | 3       | 2016-01-07 09:10:32 |

And once more, I have (must) to start query with 'SELECT * FROM drawings WHERE ......'. 再一次,我必须(必须)以“ SELECT * FROM drawings WHERE ......”开始查询。

Last thing I tried were: 我尝试的最后一件事是:

SELECT * FROM `drawings` WHERE `revision` IN ( SELECT MAX(`revision`) FROM `drawings` GROUP BY `number`, `format` ) GROUP BY `number`, `format` ORDER BY `number` DESC;

... and I got an proper pdf and wrong/lowest dwg (1 instead of 3). ...我得到了正确的pdf和错误/最低的dwg(从1改为3)。

If I read your question correctly, then you want to get drawings from each format group having the maximum revision number in that group. 如果我正确阅读了您的问题,那么您希望从每个format组中具有最大修订号的图纸中获取图纸。 One approach uses a subquery to identify formats and their max revisions, and then uses that subquery to restrict the original drawings table. 一种方法是使用子查询来标识格式及其最大修订版本,然后使用该子查询来限制原始drawings表。

SELECT t1.*
FROM drawings t1
INNER JOIN
(
    SELECT format, MAX(revision) AS revision
    FROM drawings
    GROUP BY format
) t2
    ON t1.format = t2.format AND
       t1.revision = t2.revision

Follow the link below for a running demo: 请点击以下链接以获取正在运行的演示:

SQLFiddle SQLFiddle

You can use a corelated subquery in the WHERE clause: 您可以在WHERE子句中使用相关的子查询:

SELECT d1.*
FROM `drawings` d1
WHERE `revision` = (
    SELECT MAX(`revision`)
    FROM `drawings` d2
    WHERE d2.`number` = d1.`number`
      AND d2.`format` = d1.`format`
) 
ORDER BY `number` DESC;

See it on SQLFiddle 在SQLFiddle上查看

The query will return exactly one row for each combination of number and format with the highest revision. 对于具有最高修订版本的numberformat每种组合,查询将只返回一行。 I use that combination because of your original query: GROUP BY number, format . 我使用该组合是因为您的原始查询是: GROUP BY number, format But you also wrote: "largest revision for appropriate format". 但是您还写道:“适用格式的最大修订版”。 In this case you should use: 在这种情况下,您应该使用:

SELECT d1.*
FROM `drawings` d1
WHERE `revision` = (
    SELECT MAX(`revision`)
    FROM `drawings` d2
    WHERE d2.`format` = d1.`format`
) 
ORDER BY `number` DESC;

I suggest: 我建议:

select d1.* from drawings d1 where d1.id in ( select max(d2.id) from drawings d2 group by d2.format ) 从图形d1中选择d1。*,其中d1.id在其中(从图形d2中按d2.format选择max(d2.id))

Querys using PrimaryKey are faster. 使用PrimaryKey的查询速度更快。 And you get the last record revision. 您将获得最后的记录修订。

This is the query: 这是查询:

select *
from drawings a inner join (select number, title, format, 
                            max(revision) as revision
                            from drawings 
                            group by number, title, format) b 
on a.number = b.number and a.title = b.title and a.format = b.format
and a.revision = b.revision

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM