Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

SQL - Gabungkan dua tabel dengan nilai tanggal yang berbeda

Kueri:Contoh 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

Hasil:

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


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara Menyinkronkan Database SQLServer dan Database MySQL

  2. Apakah Diucapkan "S-Q-L" atau "Sekuel"?

  3. Mencari skrip pagination php/mysql tingkat lanjut

  4. PHP MySQL Yii - database membaca bukan menulis

  5. Parameterisasi nama file di MYSQL LOAD DATA INFILE