Khelmon
Khelmon

Reputation: 19

SQL query to pull data from 2 tables

I have a SQL query I am having a hard time figuring out.

I have a list of devices stored in one table, and in another table, I have a list of the same devices that have been used at an event.

I need to get a count of all the devices that have been used in each event, even if that result is 0.

I managed to get a count of all the devices that have been used at an event as they are all in one table, however, not the zero counts as I'm not sure how to pull that data from the other table.

The query I use is

SELECT manufacturer, serial, count(event_id)
FROM event
GROUP BY event.manufacturer, event.serial
HAVING count(event_id) >= 0;

All devices are stored in another table called drone, so my question is, how do I put it all together, and get a count of all devices.

Upvotes: 0

Views: 38

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

You want a left join. Your question is a bit unclear on the data format, but something like this:

SELECT d.manufacturer, d.serial, count(e.event_id)
FROM drone d LEFT JOIN
     events e
     ON e.device_id = d.device_id
GROUP BY d.manufacturer, d.serial;

Upvotes: 2

Related Questions