2015-10-13 82 views
1

我需要選擇每個產品的第一拇指圖像(在這裏它的實體ID)SQL查詢來挑選每個ID

我的查詢

SELECT entity_id as product_id, value FROM catalog_product_entity_media_gallery 

的一個形象,但它回來時,我需要查詢到的所有圖像從每個ID挑選第一張圖片可以提供任何幫助。

db table

+0

使用的限制條款。 –

+1

'SELECT entity_id as product_id,value FROM catalog_product_entity_media_gallery LIMIT 1' – aldrin27

+0

它將限制一個產品圖像作爲一個整體我需要圖像被限制爲每個產品ID分別 –

回答

1
SELECT entity_id as product_id, value FROM catalog_product_entity_media_gallery GROUP BY entity_id 
+0

這正是這個代碼所做的 –

+0

按產品ID分組,您獲得一行產品... –

1

可以限制返回行從有限度的0指數開始1分的紀錄。

SELECT entity_id as product_id,value FROM catalog_product_entity_media_gallery GROUP BY entity_id 
+0

OP需要從每個ID獲取**一個圖像** –

1

試試這個,使用GROUP BY爲獲得第一個

SELECT entity_id as product_id, value FROM catalog_product_entity_media_gallery GROUP BY entity_id ORDER BY entity_id 
1

你麪包車使用GROUP BY條款

SELECT entity_id as product_id, value FROM catalog_product_entity_media_gallery 
GROUP BY entity_id ORDER BY entity_id 

ORDER BY ENTITY_ID取分組和順序它們第一個拇指圖像