【问题标题】:MySQL: multiple count in one rowMySQL:一行中有多个计数
【发布时间】:2016-06-06 08:07:12
【问题描述】:

我有下一个数据:

mysql> select no,crt_date,tobilling_date,sent_to_client,dop_prov from assistantfin limit 20; +--------+----------+------------------ ---+----------------+------------+ |没有 | crt_date |开票日期 | sent_to_client | dop_prov | +--------+----------+------------------ ---+----------------+------------+ | 50.01 | 2014-02-05 10:28:10 | 2014-02-05 14:42:35 | 2014-04-16 | 2014-09-23 | | 123.01 | 2014-02-05 19:17:36 | 2014-03-17 18:58:05 | 2014-04-10 | 2014-06-30 | | 51.01 | 2014-02-06 00:09:32 | 2014-03-20 16:53:46 | 2014-04-10 | 2014-06-30 | | 124.01 | 2014-02-06 15:29:08 | 2014-03-20 17:04:42 | 2014-04-10 | 2014-06-30 | | 230.01 | 2014-02-07 22:01:11 | 2014-03-20 16:41:03 | 2014-04-10 | 2014-06-30 | | 252.01 | 2014-02-08 02:52:33 | 2014-03-20 16:43:03 | 2014-04-10 | 2014-06-30 | | 123.02 | 2014-02-08 03:00:52 | 2014-03-17 18:58:10 | 2014-04-10 | 2014-06-30 | | 213.01 | 2014-02-08 04:01:35 | 2014-03-26 19:03:01 | 2014-04-10 | 2014-09-19 | | 55.01 | 2014-02-08 21:04:45 | 2014-03-07 18:40:46 |空 | 2014-06-26 | | 126.01 | 2014-02-08 21:46:58 | 2014-09-02 18:39:36 | 2014-09-09 | 2014-09-26 | | 284.01 | 2014-02-09 01:52:54 | 2014-06-11 19:11:06 | 2014-07-02 | 2014-07-21 | | 261.01 | 2014-02-09 02:20:34 | 2014-03-17 20:57:39 | 2014-04-10 | 2014-06-30 | | 318.01 | 2014-02-09 03:09:28 | 2014-03-17 20:44:25 | 2014-04-10 | 2014-06-30 | | 225.01 | 2015-02-10 03:21:08 | 2014-03-20 16:57:56 | 2014-04-10 | 2014-06-30 | | 248.01 | 2014-02-09 03:30:58 | 2014-03-18 18:02:21 | 2014-04-10 | 2014-06-30 | | 178.01 | 2014-04-05 03:35:25 | 2014-03-21 17:10:12 | 2014-04-10 | 2014-06-30 | | 184.01 | 2014-04-08 04:01:13 | 2015-03-20 16:38:02 | 2015-04-10 | 2015-06-30 | | 320.01 | 2014-04-08 05:57:23 | 2015-03-17 20:49:19 | 2015-04-10 | 2015-06-30 | | 230.02 | 2015-05-08 06:18:15 | 2016-03-20 16:41:08 | 2016-04-10 | 2016-06-06 | | 325.01 | 2014-05-09 06:23:50 | 2015-03-17 20:42:04 | 2015-04-10 | 2015-06-30 | +--------+----------+------------------ ---+----------------+------------+

需要获取下一个数据:

+---------+---------+--------+------------+-------- -+ |年份 |创建 |通过 |致客户 |证明 | +---------+---------+--------+------------+-------- -+ | 2016-01 | 1901 | 1879 | 1873 | 1743 | | 2016-02 |第2192章第2169章2114 | 1912 | | 2016-03 |第2693章2639 |第2539章2309 | | 2016-04 |第2634章第2574章第2273章1976 | | 2016-05 |第2593章第2497章1109 |第949章 | 2016-06 |第471章第449章2 | 78 | +---------+---------+--------+------------+-------- -+

其中年份为DATE_FORMAT(curdate(), '%Y-%m'),下一栏Count(assistfin.crt_date)Created

问题是crt_date 可以像2015%,但sent_to_clientdop_prov 可以像2016%

如何正确查询?

【问题讨论】:

  • 什么?你到底是怎么得到这个输出的?
  • 只是一个简单的问题,我想我知道您想要什么,并且可以使用联合来解决。但是很快,您为数据中的第二个条目(no = 123.01)发布的上述查询将在“2014-02”的 Created 列中创建一个 1,并且由于 tobilling_date 不为空,它也会在传递的同一个月的专栏。但是您实际上希望该 1 在 March 行中,因此有可能通过 > 创建?
  • 是的,一切都正确。我需要使用日期,而不是在表格字段中,而是作为单独的列表。

标签: mysql sql


【解决方案1】:

好吧,对不起,这太长太乱了,而且我不能使用工会来做,因为我在 cmets 中如此傲慢地发布,还必须引用 MySQL: Is it possible to 'fill' a SELECT with values without a table?,它给了我月份的列表。您可以重写它,以便将所有表加入 crt_date,但是当没有创建任何内容时它不会显示月份,因此生成的月份表。原始查询的月份限制为 120,但我已将其替换为 datetime > '2014',以便您更改为最早的日期。

试试这个,看看它的运行速度有多快。

select Months.yearmonth, created, passed, to_client, to_prov

from
(SELECT date_format(datetime,'%Y-%m') as yearmonth
FROM (
    select (curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) MONTH) as datetime
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) AS t
where datetime > '2014' -- enter your earliest year here
ORDER BY datetime ASC) Months left join
(select date_format(crt_date,'%Y-%m') as yearmonth, count(no) as "created" from assistfin group by yearmonth) created on Months.yearmonth=created.yearmonth
left join
(select date_format(tobilling_date,'%Y-%m') as yearmonth, count(no) as "passed" from assistfin group by yearmonth) passed on Months.yearmonth=passed.yearmonth
left join
(select date_format(sent_to_client,'%Y-%m') as yearmonth, count(no) as "to_client" from assistfin group by yearmonth) to_client on Months.yearmonth=to_client.yearmonth
left join
(select date_format(dop_prov,'%Y-%m') as yearmonth, count(no) as "to_prov" from assistfin group by yearmonth) to_prov on Months.yearmonth=to_prov.yearmonth
where 
group by yearmonth;

【讨论】:

  • 尝试在 from 语句中运行每个子查询,希望能向您展示它是如何完成的。但基本上它通过将子查询加入到自 xxxx-xx 以来的完整月份列表中来工作。因为如果您只是将它们连接在一起,您可能会遇到问题,尽管其他列包含这些时期的信息,但您的表中缺少月份却无法生成。
【解决方案2】:

在 where 中使用 group by 和 date_forma

select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y-%m') = date_format(now(), '%Y-%m')
group by year

这一年你可以

select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y') = date_format(now(), '%Y')
group by year

或者你可以在 A Range OF yearS

select  date_format(crt_date, '%Y-%m') as year, count(sent_to_client ), count(dop_pprov) 
from assistfin 
where date_format(crt_date, '%Y') 
           BETWEEN(date_format(now(),'%Y')-2) and date_format(now(), '%Y') 
group by year

【讨论】:

  • 你需要多长时间?
  • 我已经更新了今年的答案..如果您需要另一个时间范围,请告诉我
  • 谢谢回答,期间 - 年。例如 crt_date=2015-02-09 和 sent_to_client=2015-02-30 的行,不属于结果表,因为 date_format(crt_date, '%Y') = date_format(now(), '%Y')
  • 对不起,需要and sent_to_client=2016-02-30,
  • 我已经更新了 o 年的 asnwer .. 或者您可以设置您喜欢的日期.. 然后评论中的最后一个问题似乎与 OQ 无关,在这种情况下是公平的标记回答被接受和/或有用,并将新问题发布在格式正确的新问题中..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2019-01-12
相关资源
最近更新 更多