我有mysql-表稱爲產品具有以下結構/內容:MySQL的優化
id (int), public_id (int), deleted (bool), online (bool)
1 1 0 1
2 1 0 0
3 1 1 0
4 2 0 1
5 2 0 1
我的問題是,如何選擇全部,當前在線,不刪除產品。在這個例子中只記錄5(public_id 2)。相同的public_id表示相同的產品(分組)。id越高,信息越新(排序)。並且產品需要而不是已刪除(其中)。還有一些其他地方的聲明,在這種情況下與在線領域。
我需要所有方面(分組,排序和地方),但我不知道如何。
有什麼建議嗎?的
結果從Galz解釋查詢:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY nomis_houses ref online online 1 const 8086 Using where
2 DEPENDENT SUBQUERY nomis_houses index NULL house_id 4 NULL 9570 Using filesort
PS。此查詢的伎倆,但非常,非常,非常緩慢:
select * from
(select * from
(select * from products order by id desc) tmp_products_1
group by public_id) tmp_products_2
where deleted = '0' AND online = '1'
內部查詢中不允許使用「max(id)」。內部查詢應該只返回一列。 – Tim 2011-03-17 17:36:38
@Tim - 已更正,現在嘗試 – Galz 2011-03-17 17:49:03
@Galz - 結果正確,但速度太慢,無法使用。 MySQL已經忙了幾分鐘。 – Tim 2011-03-17 17:59:19