简体   繁体   English

如何在 SQL 查询中将一个表中的值用作另一表中的列名

[英]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 dep_apt_code arr_apt_code arr_apt_code date日期
34 34 ATL ATL JFK肯尼迪 2022-06-01 2022-06-01
48 48 IAD IAD SFO SFO 2022-06-02 2022-06-02

and Weather:和天气:

date日期 ATL ATL IAD IAD JFK肯尼迪 SFO SFO
2022-06-01 2022-06-01 cloudy多云的 windy有风 rainy下雨 sunny晴天
2022-06-02 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:目前,我想引用 Wea​​ther 中与 Flights.dep_apt_code 对应的列来创建如下表:

date日期 flight_num航班号 dep部门 arr arr weather天气
2022-06-01 2022-06-01 34 34 ATL ATL JFK肯尼迪 cloudy多云的
2022-06-02 2022-06-02 48 48 IAD IAD SFO 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 apt_code weather天气
2022-06-01 2022-06-01 ATL ATL cloudy多云的
2022-06-01 2022-06-01 IAD IAD windy有风
2022-06-01 2022-06-01 JFK肯尼迪 rainy下雨
2022-06-01 2022-06-01 SFO SFO sunny晴天
2022-06-02 2022-06-02 ATL ATL sunny晴天
2022-06-02 2022-06-02 IAD IAD rainy下雨
2022-06-02 2022-06-02 JFK肯尼迪 rainy下雨
2022-06-02 2022-06-02 SFO 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.

相关问题 如何在一个查询中将一个表中的行值用作另一个表的名称? - How to use a row value from one table as the name of another table in one query? 来自一个表的COLUMN NAME和COLUMN COMMENT,来自另一个表的COLUMN VALUE。 怎么样? - COLUMN NAME and COLUMN COMMENT from one table and COLUMN VALUE from another. How? 如何在一个查询中使用表中的选定值并将其用于下一个表中的下一个选择? - How to use selected value from table and use it for next selection from another table in one query? 如何将一个MySql表中的值与PHP中另一个表的列名匹配? - How do I match value from one MySql table to column name of another table in PHP? 如何使用 SQL 查询从表中检索值计数作为列名作为值名的单独列 - How to retrieve value counts from table as separate columns with column name as value name using SQL query 我可以在mysql中使用一个表列值作为另一个表表名吗? - Can I use one table column value as another table table name in mysql? 如何通过匹配列名将列数据从一个表复制到另一个表? 在 SQL 或 Power Query 中 - How to Copy column data from one table to another by matching Column names? in SQL or Power Query 使用一个表中的结果字符串作为另一查询的列名 - use result string from one table as column names for another query 从另一张表中查询两列值的SQL查询 - SQL query for two column value print from another table SQL查询,从另一个表的列中插入最大值 - SQL Query, insert a max value from another table's column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM