0
我確實有一個mysql
產品表,其中包含id,name,description和category_id列。我如何創建這個mysql select查詢?
這是選擇id,name和category_id的結果。
+----+-------------+----------------------------+
| id | category_id | name |
+----+-------------+----------------------------+
| 6 | 1 | category name |
| 7 | 2 | category name |
| 8 | 3 | category name |
| 9 | 2 | category name |
| 11 | 2 | category name |
| 15 | 3 | category name |
| 13 | 4 | category name |
| 14 | 1 | category name |
| 15 | 2 | category name |
| 16 | 2 | category name |
| 17 | 3 | category name |
| 18 | 4 | category name |
| 19 | 1 | category name |
+----+-------------+----------------------------+
我的問題是,只是我需要從上表中選擇新添加的4個產品。這4個產品應該是4個不同的類別。
這就是我的嘗試。但它不適合我。從上面的查詢
SELECT p.id
, p.category_id
, p.name
, p.description
FROM products p
WHERE p.category_id IN ('1', '2', '3', '4')
ORDER BY added_date DESC
LIMIT 4
+----+-------------+--------+-------------+
| id | category_id | name | description |
+----+-------------+--------+-------------+
| 8 | 4 | dfadff | dfasf |
| 7 | 4 | dffdsf | fdfdfaf |
| 6 | 3 | fdfdsf | fdsfdsfd |
| 5 | 2 | dffdsf | dfsfsf |
+----+-------------+--------+-------------+
4 rows in set (0.00 sec)
結果:
[檢索每個組中的最後一條記錄]的可能重複(http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group) – Strawberry