Let's say I have two tables:
Flights looks like:
| flight_num | dep_apt_code | arr_apt_code | date |
|---|---|---|---|
| 34 | ATL | JFK | 2022-06-01 |
| 48 | IAD | SFO | 2022-06-02 |
and Weather:
| date | ATL | IAD | JFK | SFO |
|---|---|---|---|---|
| 2022-06-01 | cloudy | windy | rainy | sunny |
| 2022-06-02 | sunny | rainy | rainy | windy |
where the names of the columns correspond to values in two of the columns in the Flights table.
Currently, I want to reference the column corresponding to Flights.dep_apt_code in Weather to create a table like:
| date | flight_num | dep | arr | weather |
|---|---|---|---|---|
| 2022-06-01 | 34 | ATL | JFK | cloudy |
| 2022-06-02 | 48 | IAD | SFO | rainy |
but I haven't been able to figure it out. I'm not the biggest expert on advanced queries, so even if someone can just point me to resources that may help me figure this out, it would be greatly appreciated.
I tried something like:
SELECT
F.date,
F.flight_num,
F.dep_apt_code as dep,
F.arr_apt_code as arr,
W.F.dep as weather
FROM Flights as F JOIN Weather as W
WHERE F.date = W.date;
but obviously that doesn't work, I just don't know syntactically how to do it.
How does one reference a column using a value from another table?