Join two tables where table A has a date value and needs to find the next date in B below the date in A

爷,独闯天下 提交于 2019-12-20 07:07:29

问题


I got this table "A":

| id | date       |
===================
| 1  | 2010-01-13 |
| 2  | 2011-04-19 |
| 3  | 2011-05-07 |
| .. | ...        |

and this table "B":

| date       | value |
======================
| 2009-03-29 | 0.5   |
| 2010-01-30 | 0.55  |
| 2011-08-12 | 0.67  |

Now I am looking for a way to JOIN those two tables having the "value" column in "B" mapped to the dates in "A". The tricky part for me here is that table "B" only stores the change date and the new value. Now when I need this value in table "A" the SQL needs to look back what date is the next below the date it is asking the value for.

So in the end the JOIN of those tables should look like this:

| id | date       | value |
===========================
| 1  | 2010-01-13 | 0.5   |
| 2  | 2011-04-19 | 0.55  |
| 3  | 2011-05-07 | 0.55  |
| .. | ...        | ...   |

How can I do this?


回答1:


-- Create and fill first table
CREATE TABLE `id_date` (
  `id` int(11) NOT NULL auto_increment,
  `iddate` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO `id_date` VALUES(1, '2010-01-13');
INSERT INTO `id_date` VALUES(2, '2011-04-19');
INSERT INTO `id_date` VALUES(3, '2011-05-07');

-- Create and fill second table    
CREATE TABLE `date_val` (
  `mydate` date NOT NULL,
  `myval` varchar(4) collate utf8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

INSERT INTO `date_val` VALUES('2009-03-29', '0.5');
INSERT INTO `date_val` VALUES('2010-01-30', '0.55');
INSERT INTO `date_val` VALUES('2011-08-12', '0.67');

-- Get the result table as asked in question
SELECT iddate, t2.mydate, t2.myval
FROM `id_date` t1
JOIN date_val t2 ON t2.mydate <= t1.iddate
AND t2.mydate = ( 
SELECT MAX( t3.mydate ) 
FROM `date_val` t3
WHERE t3.mydate <= t1.iddate )

What we're doing:

  • for each date in the id_date table (your table A),
  • we find the date in the date_val table (your table B)
  • which is the highest date in the date_val table (but still smaller than the id_date.date)



回答2:


You could use a subquery with limit 1 to look up the latest value in table B:

select  id
,       date
,       (
        select  value
        from    B
        where   B.date < A.date
        order by
                B.date desc
        limit   1
        ) as value
from    A



回答3:


The INNER JOIN return rows when there is at least one match in both tables. Try this.

Select A.id,A.date,b.value 
from A inner join B 
on A.date=b.date 


来源:https://stackoverflow.com/questions/8446444/join-two-tables-where-table-a-has-a-date-value-and-needs-to-find-the-next-date-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!