7

我正在研究在网站上显示股票信息的项目。我想问一下如何在 SQL 中组合两个表。

假设我们有 Table1

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

我们有 Table2

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

感谢您的帮助。

4

4 回答 4

3

查询: SQLFIDDLE示例

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) |
于 2013-04-02T13:12:12.257 回答
1
FULL JOIN ?   TABLE1 FULL JOIN TABLE2 ON T1.DATE=T2.DATE
于 2013-04-01T05:32:57.843 回答
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,因此您可以通过将 LEFT JOIN 与 RIGHT JOIN 的结果组合得到一个

于 2013-04-01T04:31:52.543 回答
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
于 2013-04-02T09:14:54.290 回答