[英]MYSQL Join WHERE Two dates column between two dates column
I have question about SQL command.我对 SQL 命令有疑问。 Simplify - My first table looks like this:简化 - 我的第一个表如下所示:
ID EAN DATE_FROM DATE_TO PRICE
1 123456 2020-01-01 2020-02-15 10
2 456789 2020-02-05 2020-02-15 20
3 987654 2020-01-10 2020-03-01 30
4 123456 2020-02-15 2020-02-17 25
And my second table look like this:我的第二张桌子看起来像这样:
ID EAN DATE_FROM DATE_TO TYPE DEALER
1 123456 2019-11-01 2020-02-15 APPLE RandomName1
2 456789 2020-01-04 2020-03-20 BANANA RandomName2
3 987654 2020-01-10 2020-03-01 ORANGE RandomName3
4 123456 2019-11-01 2020-02-17 MANGO RandomName4
And what I am doing and what I want..还有我在做什么和我想要什么..
I want to JOIN data from table 2 (type and dealer) with data from table 1. So results will looks like table 2 but with date_from and date_to from table 1 and price from table 1.我想将表 2 中的数据(类型和经销商)与表 1 中的数据相结合。因此结果将类似于表 2,但带有表 1 中的 date_from 和 date_to 以及表 1 中的价格。
Here is my SQL - But query bad joins date_from and date_to.这是我的 SQL - 但查询错误连接 date_from 和 date_to。 Doesnt not show all ean from table 1 if I am joining it from table 2.如果我从表 2 中加入它,则不会显示表 1 中的所有 ean。
SELECT t1.ean, t1.date_from, t1.date_to, t1.price, t2.type, t2.dealer
FROM table1 t1
INNER JOIN
table2 t2
ON t1.ean = t2.ean
AND t1.date_from >= t2.date_from
AND t1.date_to <= t2.date_to
How to do join better way if I am need to join two dates columns between two dates columns?如果我需要在两个日期列之间加入两个日期列,如何更好地加入?
EDIT:编辑:
Why I am need join on dates?为什么我需要参加约会? Because Dealer is changing between dates.因为经销商在日期之间变化。 EAN 123456 is from 2020-01-01 to 2020-02-15 on first trader (RandomName1) And from 2020-02-15 to 2020-02-17 on second trader (RandomName2) EAN 123456 在第一个交易者 (RandomName1) 上从 2020-01-01 到 2020-02-15,在第二个交易者 (RandomName2) 上从 2020-02-15 到 2020-02-17
So I need to join on dates because I need valid trader on that time sequence.所以我需要加入日期,因为我需要那个时间序列上的有效交易者。
In your case, the JOIN conditions on date_from
and date_to
are filtering out the row with the EAN value "123456".在您的情况下, date_from
和date_to
上的 JOIN 条件正在过滤掉 EAN 值“123456”的行。
If you don't need the date conditions, you can remove them from the JOIN an you will have all EAN rows in the result.如果您不需要日期条件,则可以将它们从 JOIN 中删除,结果中将包含所有 EAN 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.