MySQL-计算打开的项目并从不同的表中获取日期
祝大家星期五快乐,我有些事情无法处理。我以前请求过支持,发现这是我能做的最好的事情:)所以,我想根据打开和关闭两个日期计算打开项目的数量。所有数据将在t2中计数,日期将从t1开始(t1存储了大量日期,因此我使用MySQL-计算打开的项目并从不同的表中获取日期,mysql,Mysql,祝大家星期五快乐,我有些事情无法处理。我以前请求过支持,发现这是我能做的最好的事情:)所以,我想根据打开和关闭两个日期计算打开项目的数量。所有数据将在t2中计数,日期将从t1开始(t1存储了大量日期,因此我使用选择DISTINCT) 因此,各表如下: CREATE TABLE t1 ( ID int (10), Date1 date); insert into T1 values ( 1, '2018-12-17'), ( 2, '2018-12-18'), ( 3, '2018-12-
选择DISTINCT
)
因此,各表如下:
CREATE TABLE t1
(
ID int (10),
Date1 date);
insert into T1 values
( 1, '2018-12-17'),
( 2, '2018-12-18'),
( 3, '2018-12-19'),
( 4, '2018-12-19'),
( 5, '2018-12-19'),
( 6, '2018-12-20'),
( 7, '2018-12-20'),
( 8, '2018-12-21'),
( 9, '2018-12-22'),
(10, '2018-12-23'),
(11, '2018-12-24'),
(12, '2018-12-25'),
(13, '2018-12-26'),
(14, '2018-12-27'),
(15, '2018-12-28');
CREATE TABLE t2
(
ID int (10),
Open_Date date,
Close_Date date);
insert into t2 values
( 1, '2018-12-17', '2018-12-18'),
( 2, '2018-12-18', '2018-12-18'),
( 3, '2018-12-18', '2018-12-18'),
( 4, '2018-12-19', '2018-12-20'),
( 5, '2018-12-19', '2018-12-21'),
( 6, '2018-12-20', '2018-12-22'),
( 7, '2018-12-20', '2018-12-22'),
( 8, '2018-12-21', '2018-12-25'),
( 9, '2018-12-22', '2018-12-26'),
(10, '2018-12-23', '2018-12-27');
这就是我想要的结果:
+------------+------------+
| Date | Count_open |
+------------+------------+
| 17/12/2018 | 1 |
| 18/12/2018 | 3 |
| 19/12/2018 | 2 |
| 20/12/2018 | 3 |
| 21/12/2018 | 4 |
| 22/12/2018 | 4 |
| 23/12/2018 | 3 |
| 23/12/2018 | 0 |
| 25/12/2018 | 0 |
| 27/12/2018 | 0 |
| 27/12/2018 | 0 |
| 28/12/2018 | 0 |
+------------+------------+
我完全被代码弄糊涂了,需要你的帮助。也许这就是你要找的
select
x.Date1 as 'Date',
count(distinct t2.id) as 'Count_open'
from (select distinct Date1 from t1) x
left join t2 on x.Date1 between t2.Open_Date and t2.Close_Date
group by x.Date1
order by x.Date1
结果:
Date Count_open
---------- ----------
2018-12-17 1
2018-12-18 3
2018-12-19 2
2018-12-20 4
2018-12-21 4
2018-12-22 4
2018-12-23 3
2018-12-24 3
2018-12-25 3
2018-12-26 2
2018-12-27 1
2018-12-28 0
也许这就是你要找的
select
x.Date1 as 'Date',
count(distinct t2.id) as 'Count_open'
from (select distinct Date1 from t1) x
left join t2 on x.Date1 between t2.Open_Date and t2.Close_Date
group by x.Date1
order by x.Date1
结果:
Date Count_open
---------- ----------
2018-12-17 1
2018-12-18 3
2018-12-19 2
2018-12-20 4
2018-12-21 4
2018-12-22 4
2018-12-23 3
2018-12-24 3
2018-12-25 3
2018-12-26 2
2018-12-27 1
2018-12-28 0
…因此我使用SELECT DISTINCT
我看不到that@Strawberry由于表t1
没有唯一的值,因此与t2
的联接将对行计数两次(或更多)。这就是为什么我认为,DISTINCT
是有意义的。…因此我使用selectdistinct
我看不见that@Strawberry由于表t1
没有唯一的值,因此与t2
的联接将对行计数两次(或更多)。这就是为什么我认为,DISTINCT
是有意义的。它在field list.Fixed中抛出一个错误-未知列t1.Date1。然而,我认为你的预期产出是错误的。不是吗?它抛出了一个错误-字段list.Fixed中的未知列t1.Date1。然而,我认为你的预期产出是错误的。不是吗?