[英]How to write mysql query to track today's records
i need to track opened,unopened and delivered emails by date. 我需要按日期跟踪已打开,未打开和已发送的电子邮件。 suppose i sent 5 emails yesterday but nothing opened yesterday and today again i sent another 5 emails so the total emails sent today =5.
假设我昨天发送了5封电子邮件,但昨天没有打开,今天又发送了5封电子邮件,所以今天发送的电子邮件总数= 5。 opened today =3(including yesterday's 2 and today's 1 email opened .still unopened 7 which is correct).
今天打开= 3(包括昨天的2和今天打开的1电子邮件。仍然未打开7是正确的)。 But i need to display
但我需要展示
today's unopened mails count = today's sent mails- today's sent emails opened 今天未打开的邮件数=今天已发送的邮件-今天已发送的电子邮件
Here is my db structure 这是我的数据库结构
ID ||| Email ||| Event ||| date**
1 ||| AAA ||| Sent ||| 04-11-2015
2 ||| BBB ||| Sent ||| 04-11-2015
3 ||| CCC ||| Sent ||| 04-11-2015
4 ||| DDD ||| Sent ||| 04-11-2015
5 ||| EEE ||| Sent||| 04-11-2015
6 ||| FFF ||| Sent ||| 05-11-2015
7 ||| GGG ||| Sent ||| 05-11-2015
8 ||| HHH ||| Sent ||| 05-11-2015
9 ||| III ||| Sent ||| 05-11-2015
I0 ||| JJJ ||| Sent ||| 05-11-2015
11 ||| AAA ||| Open ||| 05-11-2015
12 ||| BBB ||| Open ||| 05-11-2015
13 ||| FFF ||| Open ||| 05-11-2015
Use MySQL str_to_date
or curdate
:- 使用MySQL
str_to_date
或curdate
:-
Select Email,
((select count(*) from table
where str_to_date(date,'%d-%m-%Y')=curdate() and Event='Sent')
-
(select count(*) from table
where str_to_date(date,'%d-%m-%Y')=curdate() and Event='Open')) as unopened
From table limit 1
Today i sent these emails 今天我发了这些邮件
FFF ||| FFF ||| Sent |||
已发送||| 05-11-2015
2015/05/11
GGG ||| GGG ||| Sent |||
已发送||| 05-11-2015
2015/05/11
HHH ||| HHH ||| Sent |||
已发送||| 05-11-2015
2015/05/11
III ||| III ||| Sent |||
已发送||| 05-11-2015
2015/05/11
JJJ ||| JJJ ||| Sent |||
已发送||| 05-11-2015
2015/05/11
On these emails opened only 1 在这些电子邮件上仅打开了1
FFF ||| FFF ||| Open |||
打开||| 05-11-2015
2015/05/11
So unopened 4 emails. 因此,未打开的4封电子邮件。
ofcourse these are opened today but these mails sent yesterday. 当然这些都是今天打开的,但是这些邮件是昨天发送的。
AAA ||| AAA ||| Open |||
打开||| 05-11-2015
2015/05/11
BBB ||| BBB ||| Open |||
打开||| 05-11-2015
2015/05/11
I want todays unopened= (today's sent)- (today's sent opened) 我要今天未开封=(今天已发送)-(今天已发送已打开)
unopened = 5-1(today's sent email opened) 未打开= 5-1(今天发送的电子邮件已打开)
so total i will get 4. 所以总计我会得到4。
要使用MySQL获取今天的记录,您可以尝试以下操作:
SELECT * FROM tbl_name WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.