2012-01-21 58 views
1

更新:感謝你感到沉淪,事情現在有了這個查詢的工作100%:MYSQL DISTINCT ORDER BY不給予正確的順序

SELECT e.entry_id 
FROM exp_channel_titles AS e 
INNER JOIN exp_matrix_data AS m ON m.entry_id = e.entry_id 
WHERE e.channel_id = 2 
GROUP BY e.entry_id 
ORDER BY MIN(m.col_id_2) ASC 

這裏是我的代碼。

從.home_laagste_rentes代碼從頁面

{exp:channel:entries dynamic="no" fixed_order="0{embed:entry_ids}" disable="member_data|pagination|categories"} 
<tr class="{switch="odd|even"}"> 
    <td><a href="#" title="{title}">{title}</a></td> 
    <td>{dk:lowest col="rente" decimals="2" dec_point=","}%</td> 
    <td>{count}</td> 
</tr> 
{/exp:channel:entries} 

代碼:

<table id="rowspan" cellspacing="0" class="tablesorter"> 
<thead> 
    <tr> 
     <th>Krediet aanbieder</th> 
      <th>Rente</th> 
      <th>Beoordeling</th> 
    </tr> 
</thead> 
<tbody> 
{embed="embeds/.home_laagste_rentes" 
entry_ids="{exp:query sql=" 
    SELECT DISTINCT (e.entry_id) 
    FROM exp_channel_titles AS e 
    INNER JOIN exp_matrix_data AS m ON m.entry_id = e.entry_id 
    WHERE e.channel_id = 2 
    ORDER BY m.col_id_2 ASC 
"}|{entry_id}{/exp:query}"} 
</tbody> 

col_id_2被設置爲十進制(10,4)

entry_id設置爲INT

這正在過濾重複,但它沒有給出正確的順序。它看起來像過濾重複是隨機完成...看到這個鏈接輸出: http://postimage.org/image/9vzahuuez/

+1

不是創建一個鏈接表查詢..請張貼孔查詢 – rauschen

+0

喜Rauschen,只是更新了完整的代碼.. –

+0

@TVoskuilen你可以發佈表'exp_channel_titles'和'exp_matrix_data'的SCHEMA嗎? –

回答

1

你有很多,可能是不同的,m.col_id_2值,一個e.entry_id

您的查詢甚至沒有有效的ANSI SQL,因爲ORDER BYDISTINCT後評估,因此ORDER BY m.col_id_2應該創建一個錯誤(如果你的MySQL設置了嚴格的ANSI SQL)。

解決辦法是狀態應該是用於訂購該值,使用的GROUP BY代替SELECT DISTINCT

SELECT e.entry_id 
FROM exp_channel_titles AS e 
INNER JOIN exp_matrix_data AS m ON m.entry_id = e.entry_id 
WHERE e.channel_id = 2 
GROUP BY e.entry_id 
ORDER BY MAX(m.col_id_2) ASC     --- or MIN(), or AVG() 
+0

Ypercube,非常感謝你用MIN工作:) –

0

我不知道你的表SCHEMA,但你可以嘗試這個查詢。

SELECT iTable.entry_id 
FROM 
    (SELECT DISTINCT e.entry_id, m.col_id_2 
    FROM exp_channel_titles AS e INNER JOIN exp_matrix_data AS m 
       ON m.entry_id = e.entry_id 
    WHERE e.channel_id = 2) as iTable 
ORDER BY iTable.col_id_2 ASC