'Selecting rows with id's from two other tables
Let's call this table service_status
:
+---------+----------+-------------+------------+
|service_id| output | created_at | updated_at |
+---------+----------+-------------+------------+
| 1 | non | 3434343434 | 3434343332 |
| 2 | non | 3434343434 | 3434343434 |
| 3 | non | 3434343434 | 3434343434 |
| 4 | non | 3434343434 | 3434343434 |
+---------+----------+-------------+------------+
This is table service
:
+---------+----------+-------------+------------+---------+
| host_id| service_id |output | created_at | updated_at |
+---------+----------+-------------+------------+---------+
| 12 | 1 | non | 3434343434 | 3434343332 |
| 23 | 2 | non | 3434343434 | 3434343434 |
| 34 | 3 | non | 3434343434 | 3434343434 |
| 45 | 4 | non | 3434343434 | 3434343434 |
+---------+----------+-------------+------------+---------+
And this one is called hosts
:
+---------+----------+-------------+------------+--------+----+
| host_id| name |output | created_at | updated_at |
+---------+----------+-------------+------------+---------+---+
| 12 | tesla | non | 3434343434 | 3434343332 |
| 23 | microsoft | non | 3434343434 | 3434343434 |
| 34 | apple | non | 3434343434 | 3434343434 |
| 45 | google | non | 3434343434 | 3434343434 |
+---------+----------+-------------+------------+---------+---+
The result I want a SELECT query to produce:
+---------+----------+-------------+------------+-------------+
| name | service_id |output | created_at | updated_at |
+---------+----------+-------------+------------+-------------+
| tesla | 1 | non | 3434343434 | 3434343332 |
| microsoft | 2 | non | 3434343434 | 3434343434 |
| apple | 3 | non | 3434343434 | 3434343434 |
| goole | 4 | non | 3434343434 | 3434343434 |
+---------+----------+-------------+------------+-------------+
How would I be able to achieve this type of query?
P.S.: I took inspiration from the following forum. Very similar to my question except they query from two tables: selecting rows with id from another table
Solution 1:[1]
Try to inner join
the tables to get the result you want.
select h.name, ss.service_id, ss.output , ss.created_at , ss.updated_at from service_status ss
INNER JOIN service s on ss.service_id = s.service_id
Inner Join hosts h on s.host_id = h.host_id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fonz |