简体   繁体   English

使用联接的MySQL查询

[英]mySQL query using join

I have 2 tables - 'cityname' and 'citymoisture'. 我有2个表格-“城市名称”和“城市水分”。

'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  

What I want to do is - 我想做的是-

Query for any of open, high, low and close in the moisture table by specifying the name of the city and date range. 通过指定城市的名称和日期范围来查询湿度表中的开盘价,最高价,最低价和最低价。

The following query works: 以下查询有效:

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;

But this only references the 'citymoisture' table. 但这仅引用“城市湿度”表。 I have a front end application that allows users to select the city name so in a sense I want to run a joined query that drops the results based on the city_full_name column in the cityname table. 我有一个前端应用程序,允许用户选择城市名称,因此从某种意义上讲,我想运行一个联合查询,该查询根据城市名称表中的city_full_name列删除结果。

I've tinkered around with a few join queries without success. 我已经修改了一些联接查询,但没有成功。 I have also spent the last several hours searching through join query examples without any success. 我还花了最后几个小时来搜索联接查询示例,但没有成功。

I greatly appreciate your help. 非常感谢您的帮助。

As a follow-up to Nikhil's request here is a sample output of the query suggested. 作为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)  

Where the 3rd column is the moisture data. 第三列是湿度数据。 The values for each city in the query are knitted together in the sense that they are stacked one after the other. 查询中每个城市的值以它们一个接一个地堆叠的方式编织在一起。 I would very much like the following output where the moisture data for each city appear in separate columns: 我非常喜欢以下输出,每个城市的湿度数据显示在单独的列中:

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  .....  

Try this: 尝试这个:

USE moisturedb  
SELECT citymoisture.date, citymoisture.time, citymoisture.close  
FROM citymoisture INNER JOIN cityname ON cityname.city_ID=citymoisture.city_ID
WHERE (citymoisture.date BETWEEN '2011/03/09' AND '2011/03/14') AND cityname.city_full_name='boston'
SELECT citymoisture.date, citymoisture.time, citymoisture.close 
FROM citymoisture LEFT JOIN cityname USING (city_ID)
WHERE (citymoisture.date BETWEEN '2011/03/09' AND '2011/03/14')
AND citymoisture.city_full_name='boston';

However, I think the query that you have posted in your question is the one that should be used. 但是,我认为您在问题中发布的查询是应该使用的查询。 You must be having a front-end with a drop-down for the user to select the city. 您必须具有一个带有下拉菜单的前端,以便用户选择城市。 That drop-down can be populated using cityname such that the city_ID is sent back to the server when the user selects a city. 可以使用cityname填充该下拉列表,以便在用户选择城市时将city_ID发送回服务器。 You can then use the city_ID in the original query that you have posted. 然后,您可以在已发布的原始查询中使用city_ID For reference, check the value attribute of the option tag here . 作为参考,请在此处检查option标签的value属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM