[英]How to use value from one table as column name in another table in SQL query
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.其中列的名称对应于 Flights 表中两列中的值。
Currently, I want to reference the column corresponding to Flights.dep_apt_code in Weather to create a table like:目前,我想引用 Weather 中与 Flights.dep_apt_code 对应的列来创建如下表:
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?如何使用另一个表中的值引用列?
SQL requires all identifiers are fixed in your expressions at the time the query is parsed. SQL 要求在解析查询时在表达式中固定所有标识符。 But you could do a CASE expression like this:
但是你可以像这样做一个 CASE 表达式:
SELECT
F.date,
F.flight_num,
F.dep_apt_code as dep,
F.arr_apt_code as arr,
CASE F.dep_apt_code
WHEN 'ATL' THEN W.ATL
WHEN 'IAD' THEN W.IAD
WHEN 'JFK' THEN W.JFK
WHEN 'SFO' THEN W.SFO
END AS dep_weather
FROM Flights AS F JOIN Weather AS W ON F.date = W.date;
The comment above says that you should normalize your structure.上面的评论说你应该规范你的结构。 This means to store the weather per city on individual rows instead of in columns on one row.
这意味着将每个城市的天气存储在单独的行中,而不是存储在一行的列中。
date![]() |
apt_code ![]() |
weather![]() |
---|---|---|
2022-06-01 ![]() |
ATL ![]() |
cloudy![]() |
2022-06-01 ![]() |
IAD ![]() |
windy![]() |
2022-06-01 ![]() |
JFK![]() |
rainy![]() |
2022-06-01 ![]() |
SFO ![]() |
sunny![]() |
2022-06-02 ![]() |
ATL ![]() |
sunny![]() |
2022-06-02 ![]() |
IAD ![]() |
rainy![]() |
2022-06-02 ![]() |
JFK![]() |
rainy![]() |
2022-06-02 ![]() |
SFO ![]() |
windy![]() |
Then you can get the value by joining to the row that matches both the date and the airport code:然后,您可以通过加入与日期和机场代码匹配的行来获取值:
SELECT
F.date,
F.flight_num,
F.dep_apt_code as dep,
F.arr_apt_code as arr,
W.weather AS dep_weather
FROM Flights AS F JOIN Weather AS W ON F.date = W.date AND F.dep_apt_code = W.apt_code;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.