【发布时间】:2011-07-20 22:59:15
【问题描述】:
我有 2 张桌子 - 'cityname' 和 'citymoisture'。
'cityname' has 2 columns:
-city_ID <- integer and primary key that increments automatically
-city_full_name <- character name i.e. boston, toronto, new york city etc...
'citymoisture' has 7 columns:
-city_ID <- tied to the city_ID field via a Foreign Key
-date
-time
-open
-high
-low
-close
我想做的是-
通过指定城市名称和日期范围查询湿度表中的任何开、高、低和收盘。
以下查询有效:
USE moisturedb
SELECT citymoisture.date, citymoisture.time, citymoisture.close
FROM citymoisture
WHERE (citymoisture.date BETWEEN '2011/03/09' AND '2011/03/14') AND citymoisture.city_id=5;
但这仅引用了“citymoisture”表。我有一个允许用户选择城市名称的前端应用程序,因此从某种意义上说,我想运行一个联合查询,该查询根据 cityname 表中的 city_full_name 列删除结果。
我已经修改了一些连接查询但没有成功。在过去的几个小时里,我也一直在搜索连接查询示例,但没有成功。
非常感谢您的帮助。
作为 Nikhil 请求的后续行动,此处提供了所建议查询的示例输出。
| 2011-03-10 | 03:38:00 | 0.918 |
| 2011-03-10 | 03:39:00 | 0.897 |
| 2011-03-10 | 03:40:00 | 0.917 |
| 2011-03-10 | 03:41:00 | 0.915 |
| 2011-03-10 | 03:42:00 | 0.914 |
| 2011-03-10 | 03:43:00 | 0.924 |
| 2011-03-10 | 03:44:00 | 0.922 |
| 2011-03-10 | 03:45:00 | 0.922 |
| 2011-03-10 | 03:46:00 | 0.923 |
| 2011-03-10 | 03:47:00 | 0.935 |
| 2011-03-10 | 03:48:00 | 0.953 |
| 2011-03-10 | 03:49:00 | 0.927 |
| 2011-03-10 | 03:50:00 | 0.962 |
| 2011-03-10 | 03:51:00 | 0.914 |
| 2011-03-10 | 03:52:00 | 0.935 |
+--------------+--------------+-------+
14770 rows in set (2 min 28.80 sec)
第 3 列是水分数据。查询中每个城市的值在它们一个接一个堆叠的意义上被编织在一起。我非常喜欢以下输出,其中每个城市的水分数据出现在单独的列中:
2011-03-10 03:49:00 0.935 0.935 0.935 .....
2011-03-10 03:50:00 0.935 0.935 0.935 .....
2011-03-10 03:51:00 0.935 0.935 0.935 .....
2011-03-10 03:52:00 0.935 0.935 0.935 .....
2011-03-10 03:53:00 0.935 0.935 0.935 .....
2011-03-10 03:54:00 0.935 0.935 0.935 .....
【问题讨论】: