7

I'm working on project to display stock information in a website. And i want to ask how to combine two tables in SQL.

Suppose we have 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

And we have 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

And i want to combine the date and display all the data like this,

Update: I want to combine the date and the 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

Thank you for your help.

kaitosenpai
  • 973
  • 1
  • 7
  • 12

4 Answers4

3

Query: 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

Result:

| 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) |
Justin
  • 9,634
  • 6
  • 35
  • 47
  • If stock_id 1 and 2 have different values, the SQL doesn't work well. Here is what i try. [SQL Fiddle](http://sqlfiddle.com/#!2/1899d/1) – kaitosenpai Apr 03 '13 at 03:50
  • I've already found the answer. Your SQL code is almost right. Here is what i try again. [SQL Fiddle](http://sqlfiddle.com/#!2/1899d/2). I add `t1.stock_id = t2.stock_id` after `LEFT JOIN` and `RIGHT JOIN` Thank you for your help. – kaitosenpai Apr 03 '13 at 04:04
1
FULL JOIN ?   TABLE1 FULL JOIN TABLE2 ON T1.DATE=T2.DATE
Anthon
  • 69,918
  • 32
  • 186
  • 246
Gentlezerg
  • 286
  • 2
  • 9
0

Something like this:

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

Since MySQL does not have the FULL OUTER JOIN, you can get one by combining the results of LEFT JOIN with RIGHT JOIN

Vaibhav Desai
  • 2,618
  • 1
  • 16
  • 16
  • I've tried your suggestion in phpMyAdmin. But it creates two columns stock_id and two columns date. How to put it in one column? Here is the snapshot of what i've tried. [link](http://imgur.com/6dVK7Fp) – kaitosenpai Apr 01 '13 at 06:01
  • Instead of using a * in the select clause, only use the coulmns you need. – Vaibhav Desai Apr 01 '13 at 08:05
  • Yes. I've already figured it out what you mean. Now, if i want to display only stock_id number 3. Where should i put the WHERE clause? I've tried to put two WHERE clauses after LEFT JOIN and after RIGHT JOIN, but it still messed up. – kaitosenpai Apr 02 '13 at 02:57
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
Vinoth
  • 33
  • 1
  • 9