gpt4 book ai didi

mysql日报表查询

转载 作者:行者123 更新时间:2023-11-29 00:58:01 25 4
gpt4 key购买 nike

下面的查询为我提供了设备租赁公司的元素报告。这是一个 super 复杂的查询,需要将近 20 秒才能运行。这显然不是获取我正在寻找的数据的正确方法。我从 PHP 构建此查询并添加开始日期 02-01-2011 和结束日期 03-01-2011、产品代码 (p_code = 1) 和产品池 (i_pool =1)。这 4 条信息被传递到一个 PHP 函数并注入(inject)到以下 sql 中以返回我需要的报告,用于显示有多少项目已用完的日历控件。我的问题是:是否有任何方法可以简化或更好地执行此操作,或者更有效地运行,使用更好的连接或更好的方式来显示各个日期。

SELECT   DISTINCT reportdays.reportday, count(*)
FROM
(SELECT '2011-02-01' + INTERVAL a + b DAY reportday
FROM
(SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9 ) d,
(SELECT 0 b UNION SELECT 10 UNION SELECT 20
UNION SELECT 30 UNION SELECT 40) m
WHERE '2011-02-01' + INTERVAL a + b DAY < '2011-03-01'
ORDER BY a + b) as reportdays
JOIN rental_inv as line
ON DATE(FROM_UNIXTIME(line.ri_delivery_dte)) <= reportdays.reportday
AND DATE(FROM_UNIXTIME(line.ri_pickup_dte)) >= reportdays.reportday
LEFT OUTER JOIN rental_in as rent on line.ri_num = rent.ri_num
LEFT OUTER JOIN rental_cancels cancelled on rent.ri_num = cancelled.ri_num
LEFT OUTER JOIN inv inventory on line.i_num = inventory.i_num
LEFT OUTER JOIN product ON inventory.p_code = product.p_code
WHERE rent.ri_extend = 0 -- disregard extended rentals
AND cancelled.ri_num is null -- disregard cancelled rentals
AND inventory.p_code = 1
AND inventory.i_pool = 1

GROUP BY reportdays.reportday

如果需要任何其他信息,请告诉我,我会发布。

最佳答案

您可以使用:

SELECT DATE(ri_delivery) as day,
count(*) as itemsout,
FROM rental_inv
GROUP BY day;

我不确定您是否需要这个或其他东西。

  SELECT dates.day, count (*)
FROM rental_inv line
INNER JOIN (SELECT DATE(ri_delivery_dte) as day FROM rental_inv
WHERE ri_delivery_dte >= '2011/02/01'
AND ri_delivery_dte <= '2011/02/28'
GROUP BY day
UNION
SELECT DATE(ri_pickup_dte) as day FROM rental_inv
WHERE ri_pickup_dte >= '2011/02/01'
AND ri_pickup_dte <= '2011/02/28'
GROUP BY day) dates
ON line.ri_delivery_dte <= dates.day and line.ri_pickup_dte >= dates.day
LEFT JOIN rental_cancels canc on line.ri_num = canc.ri_num
LEFT JOIN rental_in rent on line.ri_num = rent.ri_num
WHERE canc.ri_num is null
AND rent.ri_extend = 0
GROUP BY dates.day

关于mysql日报表查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5082288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com