2013-04-01 146 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 
相關問題