我有一个MySQL查询,它为一个对象及其所有子对象创建一对多的数据。我的查询如下:
select * from object o
left join event e on o.tenant_id = e.tenant_id AND o.udid = e.udid
left join event_type et on e.event_type_id = et.event_type_id
left join event_custom_field ecf on e.event_id = ecf.event_id
left join profile p on o.tenant_id = p.tenant_id AND o.udid = p.udid
left join status s on o.tenant_id = s.tenant_id AND o.udid = s.udid
left join o_tenant_app ota on o.tenant_id = ota.tenant_id AND o.udid = ota.udid
left join tenant_app ta on ota.tenant_app_id = ta.tenant_app_id
left join heartbeat h on o.tenant_id = h.tenant_id AND o.udid = h.udid
where o.tenant_id = 'a'
AND o.checked_out = false
and p.name in ('test')
and s.status_name in ('stat1', 'stat2')
order by o.udid, e.event_id;
我需要能够对返回的对象数量应用限制,同时仍然包含所有其他已连接的数据。如果我把限制放在最后,那么我将丢失对象数据。我可以在我的第一行做到这一点:
select * from (select * from object limit 0,5) o
但如果前5个对象与where子句不匹配,那么将显示少于5个对象。我想尽可能地做到这一点,因为我可能会有数百万条记录要查询。