Mysql 在问题中进行解释,以便我们看到。尝试日历左连接收据左连接位置。或者尝试先使用日历,然后再使用剩余连接。中间的无法加快速度。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。之间的在性能上总是相当于=。(在功能上不等同于,中间是包含在内的。)中间的没
Mysql 在问题中进行解释,以便我们看到。尝试日历左连接收据左连接位置。或者尝试先使用日历,然后再使用剩余连接。中间的无法加快速度。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。之间的在性能上总是相当于=。(在功能上不等同于,中间是包含在内的。)中间的没,mysql,sql,database,performance,optimization,Mysql,Sql,Database,Performance,Optimization,在问题中进行解释,以便我们看到。尝试日历左连接收据左连接位置。或者尝试先使用日历,然后再使用剩余连接。中间的无法加快速度。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。之间的在性能上总是相当于=。(在功能上不等同于,中间是包含在内的。)中间的没有加速。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。之间的在性能上总是相当于=。(并且在功能上不等同于,包含在两者之间。)感谢您的帮助。我尝试了你发布的第一个查询,但仍然需要很长时间才能运行。然而,我最终自己解决了这个问题(见我的
在问题中进行解释,以便我们看到。尝试日历左连接收据左连接位置。或者尝试先使用日历,然后再使用剩余连接。中间的
无法加快速度。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。
之间的在性能上总是相当于=
。(在功能上不等同于
,中间是包含在内的。)中间的没有加速。然而,我最终自己解决了这个问题(见答案)。不过还是要感谢。
之间的在性能上总是相当于=
。(并且在功能上不等同于
,包含在两者之间。)感谢您的帮助。我尝试了你发布的第一个查询,但仍然需要很长时间才能运行。然而,我最终自己解决了这个问题(见我的答案)。我必须将where语句移动到两个join子查询中,这就解决了我的问题。我怀疑这是因为它不再试图在一个已经很大的日历日期表中加入这么多行。将结果集限制为join解决了此问题。我不知道这是为什么,但嘿,这是有效的!谢谢你的帮助。我尝试了你发布的第一个查询,但仍然需要很长时间才能运行。然而,我最终自己解决了这个问题(见我的答案)。我必须将where语句移动到两个join子查询中,这就解决了我的问题。我怀疑这是因为它不再试图在一个已经很大的日历日期表中加入这么多行。将结果集限制为join解决了此问题。我不知道这是为什么,但嘿,这是有效的!
SELECT
`locations`.`name` AS `location`,
`calendar`.`date` AS `date`,
COUNT(`receipts`.`id`) AS `count`
FROM `locations`
CROSS JOIN `calendar`
LEFT JOIN `receipts` ON `calendar`.`date` = DATE(`receipts`.`datetime`)
AND `locations`.`id` = UPPER(LEFT(`receipts`.`id`, 1)) # there is no `location_id` FK. First char of receipts id is same as location id
WHERE `calendar`.`date` >= '2017-04-01' AND `calendar`.`date` <= '2017-04-07'
GROUP BY `locations`.`id`, `calendar`.`id`
ORDER BY `locations`.`name` ASC, `calendar`.`date` ASC;
SELECT
`locations`.`name` AS `location`,
`calendar`.`date` AS `date`,
COUNT(`receipts`.`id`) AS `count`
FROM `locations`
CROSS JOIN `calendar`
LEFT JOIN `receipts` ON `calendar`.`date` = DATE(`receipts`.`datetime`)
AND `locations`.`id` = UPPER(LEFT(`receipts`.`id`, 1)) # there is no `location_id` FK. First char of receipts id is same as location id
WHERE DATE(`receipts`.`datetime`) >= '2017-04-01' AND DATE(`receipts`.`datetime`) <= '2017-04-07'
GROUP BY `locations`.`id`, `calendar`.`id`
ORDER BY `locations`.`name` ASC, `calendar`.`date` ASC;
SELECT
`locations`.`name` AS `location`,
`cal`.`date` AS `date`,
COUNT(`receipts`.`id`) AS `count`
FROM `locations`
CROSS JOIN (
SELECT `calendar`.`id`, `calendar`.`date`
FROM `calendar`
WHERE `calendar`.`date` >= '2017-04-01' AND `calendar`.`date` <= '2017-04-07'
) `cal`
LEFT JOIN `receipts` ON `cal`.`date` = DATE(`receipts`.`datetime`)
AND `locations`.`id` = UPPER(LEFT(`receipts`.`id`, 1)) # there is no `location_id` FK. First char of receipts id is same as location id
WHERE DATE(`receipts`.`datetime`) >= '2017-04-01' AND DATE(`receipts`.`datetime`) <= '2017-04-07'
GROUP BY `locations`.`id`, `cal`.`id`
ORDER BY `locations`.`name` ASC, `cal`.`date` ASC;
SELECT
`locations`.`name` AS `location`,
`calendar`.`date` AS `date`,
COUNT(`receipts`.`id`) AS `count`
FROM `locations`
CROSS JOIN `calendar`
LEFT JOIN `receipts` ON `calendar`.`date` = DATE(`receipts`.`datetime`)
AND `locations`.`id` = UPPER(LEFT(`receipts`.`id`, 1)) # there is no `location_id` FK. First char of receipts id is same as location id
WHERE `calendar`.`date` BETWEEN '2017-04-01' AND '2017-04-07'
GROUP BY `locations`.`id`, `calendar`.`id`
ORDER BY `locations`.`name` ASC, `calendar`.`date` ASC;
SELECT
`l`.`name` AS `location`,
`c`.`date` AS `date`,
COUNT(`r`.`id`) AS `count`
FROM `locations` AS `l`
CROSS JOIN (
SELECT `calendar`.`id`, `calendar`.`date`
FROM `calendar`
WHERE `calendar`.`date` >= '2017-04-01' AND `calendar`.`date` <= '2017-04-07'
) `c`
LEFT JOIN (
SELECT `receipts`.`id`, `receipts`.`datetime`
FROM `receipts`
WHERE DATE(`receipts`.`datetime`) >= '2017-04-01' AND DATE(`receipts`.`datetime`) <= '2017-04-07'
) `r` ON `c`.`date` = DATE(`r`.`datetime`) AND `l`.`id` = UPPER(LEFT(`r`.`id`, 1))
GROUP BY `l`.`id`, `c`.`id`
ORDER BY `l`.`name` ASC, `c`.`date` ASC;
SELECT l.name location, c.date, COUNT(r.id) count
FROM calendar c
left join calendar n on n.Date = c.Date + 1 -- one day after c.date
left join (locations l join receipts r
on r.id like '%' + l.Id)
on r.datetime between c.Date and n.Date
where c.Date between '2017-04-01' and '2017-04-07'
GROUP BY l.id, c.id
ORDER BY l.name, c.date;
SELECT l.name location, c.date, COUNT(r.id) count
FROM calendar c
left join calendar n on n.Date =
(Select min(date) from Calendar -- subquery gets the next day in calendar
Where date > c.Date)
left join (locations l join receipts r
on r.id like '%' + l.Id)
on r.datetime between c.Date and n.Date
where c.Date between '2017-04-01' and '2017-04-07'
GROUP BY l.id, c.id
ORDER BY l.name, c.date;