Reputation: 95
I'm breaking my head trying to modify this query(thx sgeddes) in order to get at result not only from db_events.events fields, instead join it with some db_system.devices fields
SELECT e.*
FROM db_events.events e
JOIN (
SELECT Max(id) MaxId, device_id
FROM db_events.events
GROUP BY device_id ) e2 on e.Id = e2.MaxId AND e.device_id = e2.device_id
WHERE e.device_id IN (
SELECT device_id
FROM db_system.devices
WHERE vendor = 1)
ORDER BY e.id DESC
How can I get it without repeat the subquery:
SELECT *
FROM db_system.devices
WHERE vendor = 1
I need get db_system.devices.brand
and db_system.devices.model
joined with final results, and
I tried to modify it step to step, I tried with temporary tables, I suspect it should be something simple, but I have not been able to do it, of course thank you very much...
Upvotes: 0
Views: 49
Reputation: 263803
is this what you want?
SELECT e.*, a.*
FROM db_events.events e
INNER JOIN
(
SELECT Max(id) MaxId, device_id
FROM db_events.events
GROUP BY device_id
) e2 on e.Id = e2.MaxId AND
e.device_id = e2.device_id
INNER JOIN db_system.devices a
ON e.device_id = a.device_id AND
a.vendor = 1
ORDER BY e.id DESC
the condition a.vendor = 1
can also be moved on WHERE
clause and the result is still the same since you are using INNER JOIN
SELECT ....
FROM .... JOIN ....
WHERE a.vendor = 1
ORDER BY ...
Upvotes: 3