我有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