1

My (sub)query results in following dataset:

+---------+------------+-----------+
| item_id | version_id | relevance |
+---------+------------+-----------+
|       1 |          1 |        30 |
|       1 |          2 |        30 |
|       2 |          3 |        22 |
|       3 |          4 |        30 |
|       4 |          5 |        18 |
|       3 |          6 |        30 |
|       2 |          7 |        22 |
|       1 |          8 |        30 |
|       5 |          9 |        48 |
|       4 |         10 |        18 |
|       5 |         11 |        48 |
|       3 |         12 |        30 |
|       3 |         13 |        31 |
|       4 |         14 |        19 |
|       2 |         15 |        22 |
|       1 |         16 |        30 |
|       5 |         17 |        49 |
|       2 |         18 |        22 |
+---------+------------+-----------+
18 rows in set (0.00 sec)

Items and versions are stored in separate InnoDB-tables. Both tables have auto-incrementing primary keys. Versions have a foreign key to items (item_id).

My question: How do I get a subset based on relevance?

I would like to fetch the following subset containing the most relevant versions:

+---------+------------+-----------+
| item_id | version_id | relevance |
+---------+------------+-----------+
|       1 |         16 |        30 |
|       2 |         18 |        22 |
|       3 |         13 |        31 |
|       4 |         14 |        19 |
|       5 |         17 |        49 |
+---------+------------+-----------+

It would be even more ideal to fetch the MAX(version_id) in case of equal relevance.

I tried grouping, joining, ordering, etcetera in many ways but I'm not able to get the desired result. Some of the things I tried is:

SELECT    item_id, version_id, relevance
FROM      (subquery) a
GROUP BY  item_id
ORDER BY  relevance DESC, version_id DESC

But of course the ordering happens after the fact, so that both relevance and MAX(version_id) information is lost.

Please advice.

4

1 回答 1

1

您可以这样做:

SELECT t1.item_id, max(t1.version_id), t1.relevance FROM t t1
LEFT JOIN t t2 ON t1.item_id = t2.item_id AND t1.relevance < t2.relevance
WHERE t2.relevance IS NULL
GROUP BY t1.item_id
ORDER BY t1.item_id, t1.version_id

输出:

| ITEM_ID | VERSION_ID | RELEVANCE |
|---------|------------|-----------|
|       1 |         16 |        30 |
|       2 |         18 |        22 |
|       3 |         13 |        31 |
|       4 |         14 |        19 |
|       5 |         17 |        49 |

在这里拉小提琴。

于 2013-10-04T07:03:13.090 回答