我有以下查询,它返回正确的结果,但我确信这不是获取这些结果的最佳方法...
select * from (
select * from features where feature_area = 0
order by updateStamp desc limit 1
) as feature_1
union all
select * from (
select * from features where feature_area = 1
order by updateStamp desc limit 1
) as feature_2
union all
select * from (
select * from features where feature_area = 2
order by updateStamp desc limit 1
) as feature_3
这返回的结果看起来像......
id feature_area title updateStamp
--------------------------------------------------------------------
103 0 This is a title 2014-04-15 09:26:14
102 1 Another title 2014-03-27 14:09:49
98 2 More title 2014-01-21 16:00:55
是否可以使用联接而不是联合来改进这一点,如果可以,请您指出正确的方向。
编辑:
查看了@Ben 指出的其他选项后,我似乎已经为我的特定目的获得了最快的查询(尽管不是那么有吸引力)。如果您认为我错了,请随时纠正我。我不是专家,因此我寻求建议。
最佳答案
select f.* from features f
inner join (
select
feature_area
max(updateStamp) as updateStamp
from
features
where feature_are IN (0,1,2)
group by feature_area
) sq on sq.feature_area = f.feature_area
and sq.updateStamp = f.updateStamp
关于mysql - 不使用 UNION 重写 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219828/