Well, I have three different tables (check below). I wanted to make that I can see, which is the latest image (req_image_1, etc) saved for each category, which is not parent (cat_parent = 0)
One table, which holds general information about requests
+----+--------------------------+------------+
| id | req_name | req_parent |
+----+--------------------------+------------+
| 3 | Send pack | 19 |
| 4 | Go Visit | 18 |
| 5 | Stop by | 19 |
| 6 | Deliver cookies | 34 |
+----+--------------------------+------------+
Second table, which holds meta information about requests
+----------+------------+------------+----------------------------+
| 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 |
+----------+------------+------------+----------------------------+
Third table, which holds information about categories
+----+------------------------+------------+
| 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 |
+----+------------------------+------------+
So far I managed, that I could get all images for each category with this query:
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_%'
I got this result:
+------------+----+-----------------------+
| category | ID | image |
+------------+----+-----------------------+
| 1 | 3 | a1c8f69edb37bf6c6.jpg |
| 1 | 4 | a71f4160d7f7f7555.jpg |
| 6 | 6 | 854b9faaa53d8fe02.jpg |
+------------+----+-----------------------+
But I wanted to make enhancement, so I would get only result, where each category has only one image against, for example category 1, has image a71f4160d7f7f7555.jpg and category 6 has image 854b9faaa53d8fe02.jpg
I bet, that I miss some basic knowledge, and simple enhancement with subquery and selecting MAX would work as a charm.
Thanks!