2016-07-07 125 views
-7

檢查附加圖像我有更多的圖像對應於jy_id。select sql distinct

結果數據應該是: -

jy_id jy_tour_book_picture 
70 xxxxxx.jpg 
83 xxxxxx.jpg 

使用查詢

select jy_id, jy_tour_book_picture from table; 

但此查詢返回: -

jy_id jy_tour_book_picture 
70 xxxxxx.jpg 
70 xxxxxx.jpg 
83 xxxxxx.jpg 
83 xxxxxx.jpg 
83 xxxxxx.jpg 

Screenshot of data

+0

在我看來,或者你只有一個,或者幾乎全部都是重複的。 – sagi

+0

Cud你可以用適當的細節和SQL代碼重新提出問題。也請刪除的PHP標籤,因爲我沒有看到任何PHP – Iceman

+0

如果有幾個圖像的id,你必須告訴我們哪一個選擇! – jarlh

回答

-1

嘗試查詢麗柯本

select * from images group by jy_id 

,或者你可以嘗試: -

select distinct jy_id, jy_tour_book_picture from images 
+3

接受的答覆?!?這兩個查詢是絕對錯誤的.. – jarlh

+0

#2是與#1相同,只是增加了一個無用的DISTINCT,至少它在MySQL中工作:) – dnoeth

+0

是#2是一樣的,我的錯誤。 –

0

您可以使用限制或最大或最小或頂部的選項只得到一個價值

例如:

SELECT image FROM TABLE WHERE id IN(83,84,85) limit 1 
-1

使用NOT EXISTS可以從表中返回一行,只要沒有其他具有相同jy_id的行具有更高的值jy_tour_book_id:

select * from tablename t1 
where not exists (select 1 from tablename t2 
        where t2.jy_id = t1.jy_id 
        and t2.jy_tour_book_id > t1.jy_tour_book_id)