2013-04-01 104 views
4

我正在进行项目以显示网站中的股票信息。我想问如何在SQL中结合两个表。SQL - 结合具有不同日期值的两个表格

假设我们有表1

stock_id  date  p_high p_low 
------------------------------------ 
3   2013-02-26  100  80 
3   2013-02-25  100  80 
3   2013-02-24  100  80 
1   2013-02-24  100  80 
3   2013-02-23  100  80 
2   2013-02-23  100  80 

而且我们有表2

stock_id  date  open high low close volume 
--------------------------------------------------------- 
3   2013-02-24  90 110 70 90  250 
3   2013-02-23  90 110 70 90  250 
2   2013-02-23  90 110 70 90  250 
3   2013-02-22  90 110 70 90  250 
3   2013-02-21  90 110 70 90  250 
1   2013-02-21  90 110 70 90  250 

,我想日期结合,像这样显示的所有数据,

更新:我想结合日期和stock_id

stock_id  date  open high low close volume p_high p_low 
------------------------------------------------------------------------ 
3   2013-02-26          100 80 
3   2013-02-25          100 80 
3   2013-02-24  90 110 70 90  250  100 80 
3   2013-02-23  90 110 70 90  250  100 80 
3   2013-02-22  90 110 70 90  250 
3   2013-02-21  90 110 70 90  250 

谢谢你的帮助。

+3

你想要一个FULL OUTER JOIN。 MySQL是否支持他们?如果不是,则加入联合权限加入。 –

+0

http://stackoverflow.com/questions/4796872/full-outer-join-in-mysql 看一看 –

回答

2

查询JOIN赠一: SQLFIDDLEExample

SELECT a.stock_id, 
     a.date, 
     a.open, 
     a.high, 
     a.low, 
     a.close, 
     a.volume, 
     a.p_high, 
     a.p_low 
FROM (
SELECT t1.stock_id, 
     t1.date, 
     t2.open, 
     t2.high, 
     t2.low, 
     t2.close, 
     t2.volume, 
     t1.p_high, 
     t1.p_low 
FROM table1 t1 
LEFT JOIN table2 t2 ON t1.date = t2.date 
UNION 
SELECT t2.stock_id, 
     t2.date, 
     t2.open, 
     t2.high, 
     t2.low, 
     t2.close, 
     t2.volume, 
     t1.p_high, 
     t1.p_low 
FROM table1 t1 
RIGHT JOIN table2 t2 ON t1.date = t2.date) a 
WHERE a.stock_id = 3 

结果:

| STOCK_ID |       DATE | OPEN | HIGH | LOW | CLOSE | VOLUME | P_HIGH | P_LOW | 
------------------------------------------------------------------------------------------------------------- 
|  3 | February, 26 2013 00:00:00+0000 | (null) | (null) | (null) | (null) | (null) | 100 |  80 | 
|  3 | February, 25 2013 00:00:00+0000 | (null) | (null) | (null) | (null) | (null) | 100 |  80 | 
|  3 | February, 24 2013 00:00:00+0000 |  90 | 110 |  70 |  90 | 250 | 100 |  80 | 
|  3 | February, 23 2013 00:00:00+0000 |  90 | 110 |  70 |  90 | 250 | 100 |  80 | 
|  3 | February, 22 2013 00:00:00+0000 |  90 | 110 |  70 |  90 | 250 | (null) | (null) | 
|  3 | February, 21 2013 00:00:00+0000 |  90 | 110 |  70 |  90 | 250 | (null) | (null) | 
+0

如果stock_id 1和2具有不同的值,则SQL无法正常工作。这是我的尝试。 [SQL小提琴](http://sqlfiddle.com/#!2/1899d/1) – kaitosenpai

+0

我已经找到答案。你的SQL代码几乎是正确的。这是我再次尝试。 [SQL Fiddle](http://sqlfiddle.com/#!2/1899d/2)。我在'LEFT JOIN'和'RIGHT JOIN'后面添加't1.stock_id = t2.stock_id'谢谢你的帮助。 – kaitosenpai

0

事情是这样的:

SELECT * 
FROM table1 LEFT JOIN table2 
ON  table1.date = table2.date 
UNION 
SELECT * 
FROM table1 RIGHT JOIN table2 
ON  table1.date = table2.date 

由于MySQL不具备FULL OUTER JOIN,您可以通过左侧的结果结合起来RIGHT JOIN

+0

我试图在phpMyAdmin你的建议。但它创建了两列stock_id和两列日期。如何把它放在一列中?这是我尝试过的快照。 [链接](http://imgur.com/6dVK7Fp) – kaitosenpai

+0

而不是在select子句中使用*,只使用你需要的coulmns。 –

+0

是的。我已经知道你的意思了。现在,如果我只想显示stock_id号码3.我应该在哪里放置WHERE子句?我试图在LEFT JOIN和RIGHT JOIN之后放置两个WHERE子句,但它仍然搞砸了。 – kaitosenpai

1
FULL JOIN ? TABLE1 FULL JOIN TABLE2 ON T1.DATE=T2.DATE 
0
select t1.stock_id,t1.date,t1.p_high,t1.p_low,t2.open,t2.high,t2.low,t2.close,t2.volume from t1 left join t2 on (0) where t1.stock_id=3 
union 
select t2.stock_id,t2.date,t1.p_high,t1.p_low,t2.open,t2.high,t2.low,t2.close,t2.volume from t2 left join t1 on (0) where t2.stock_id=3 
相关问题