选择MAX值的行

时间:2012-10-09 14:24:06

标签: mysql select join max

好吧,我有三张不同的牌桌(请查看下方)。我想让我能看到,这是为每个类别保存的最新图像(req_image_1等),它不是父类(cat_parent = 0)

一个表,其中包含有关请求的一般信息

+----+--------------------------+------------+
| id | req_name                 | req_parent |
+----+--------------------------+------------+
|  3 | Send pack                |         19 |
|  4 | Go Visit                 |         18 |
|  5 | Stop by                  |         19 |
|  6 | Deliver cookies          |         34 |
+----+--------------------------+------------+

第二个表,其中包含有关请求的元信息

+----------+------------+------------+----------------------------+
| umeta_id | request_id | meta_key   | meta_value                 |
+----------+------------+------------+----------------------------+
|       1  |          3 | req_city   | London                     |
|       2  |          3 | req_street | 11 Baker street            |
|       3  |          3 | req_img_1  | a1c8f69edb37bf6c6.jpg      |
|       4  |          4 | req_city   | Manchester                 |
|       5  |          4 | req_street | 71 Main street             |
|       6  |          4 | req_img_2  | a71f4160d7f7f7555.jpg      |
|       7  |          5 | req_city   | Sheffield                  |
|       8  |          5 | req_street | 240 Duke street            |
|       9  |          6 | req_city   | Manchester                 |
|       10 |          6 | req_street | 13 Chapel street           |
|       11 |          6 | req_img_1  | 854b9faaa53d8fe02.jpg      |
+----------+------------+------------+----------------------------+

第三个表,其中包含有关类别的信息

+----+------------------------+------------+
| ID | cat_name               | cat_parent |
+----+------------------------+------------+
|  1 | Category_01            |          0 |
|  6 | Category_02            |          0 |
| 18 | Category_01_01         |          1 |
| 19 | Category_01_02         |          1 |
| 34 | Category_02_01         |          6 |
+----+------------------------+------------+

到目前为止,我管理过,我可以使用此查询获取每个类别的所有图像:

SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
FROM d_requests req
LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id ) 
LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID ) 
WHERE meta.meta_key LIKE 'req_img_%'

我得到了这个结果:

+------------+----+-----------------------+
| category   | ID | image                 |
+------------+----+-----------------------+
|          1 |  3 | a1c8f69edb37bf6c6.jpg |
|          1 |  4 | a71f4160d7f7f7555.jpg |
|          6 |  6 | 854b9faaa53d8fe02.jpg |
+------------+----+-----------------------+

但是我想要进行增强,所以我只得到结果,每个类别只有一个图像,例如类别1,图像a71f4160d7f7f7555.jpg,类别6有图像854b9faaa53d8fe02.jpg

我打赌,我错过了一些基本知识,使用子查询进行简单的增强,选择MAX就可以作为魅力。

谢谢!

3 个答案:

答案 0 :(得分:2)

SQL Fiddle

select 
    category, 
    (select request_id 
     from d_requests_meta 
     where umeta_id = s.ID 
    ) as ID,
    (select meta_value 
     from d_requests_meta 
     where umeta_id = s.ID 
    ) AS image
from (
    SELECT cat.cat_parent AS category, max(meta.umeta_id) ID
    FROM d_requests req
    LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id ) 
    LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID ) 
    WHERE meta.meta_key LIKE 'req_img_%'
    group by cat.cat_parent
) s

答案 1 :(得分:1)

SELECT category, ID, image
FROM (  SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
        FROM d_requests AS req
        LEFT JOIN d_requests_meta AS meta
        ON req.ID = meta.request_id
        LEFT JOIN d_categories AS cat
        ON req.req_parent = cat.ID
        WHERE meta.meta_key LIKE 'req_img_%'
        ORDER BY req.ID DESC) AS h
GROUP BY category

我使用非官方的MySQL假设编辑了Clodoaldo的答案,GROUP BY将在子查询中基于ORDER BY返回第1行。

答案 2 :(得分:0)

试试这个http://sqlfiddle.com/#!2/bfe9a/19

    SELECT Category, ID, Image FROM (
    SELECT Category, ID, Image, 
    @id:=CASE WHEN @category <> category THEN 1 ELSE @id+1 END AS ImgRank,
    @category:=category AS categoryTemp   FROM  
    (SELECT @id:= 0) AS i,
    (SELECT @category:= 0) AS c,    
    (
        SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
        FROM d_requests req
        LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id ) 
        LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID ) 
        WHERE meta.meta_key LIKE 'req_img_%'
        ORDER BY cat.cat_parent, req.id desc
    ) Vw
  ) vw2 WHERE IMGRANK = 1