简体   繁体   English

从 2 个表中提取数据的 SQL 查询

[英]SQL query to pull data from 2 tables

I have a SQL query I am having a hard time figuring out.我有一个 SQL 查询,我很难弄清楚。

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.我需要计算每个事件中使用过的所有设备,即使结果为 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.所有设备都存储在另一个名为无人机的表中,所以我的问题是,我如何将它们放在一起,并获得所有设备的数量。

You want a left join .你想要一个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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM