gpt4 book ai didi

php - mysql用循环计数行

转载 作者:行者123 更新时间:2023-11-29 04:22:36 27 4
gpt4 key购买 nike

我有下表:

+-------------+--------------+ 
| product | purchased |
+-------------+--------------+
| Hammer | <timestamp> |
| Nipper | <timestamp> |
| Saw | <timestamp> |
| Nipper | <timestamp> |
| Hammer | <timestamp> |
| Hammer | <timestamp> |
| Saw | <timestamp> |
| Saw | <timestamp> |
| Saw | <timestamp> |
+-------------+--------------+

我想查看有关这些产品保修状态的摘要。保修期应为自购买之日起 5 年。因此,对于今天的日期,应该可以检查产品是否在保修期内(基于购买日期)。另外,我现在想知道最后一次购买的产品何时会超出保修范围。这是一个示例表:

+-------------+--------------+----------------+------------------+------------------+
| product | count | warranty valid | warranty expired | last p. warranty |
+-------------+--------------+----------------+------------------+------------------+
| Hammer | 3 | 1 | 2 | 10.03.2015 |
| Nipper | 2 | 2 | - | 01.01.2014 |
| Saw | 4 | 1 | 3 | 02.12.2013 |
+-------------+--------------+----------------+------------------+------------------+

我试图为这个例子创建查询,但我不知道如何循环每个产品的结果来计算有效保修等。这就是我现在所拥有的:

SELECT
product,
date_format(from_unixtime(purchased), '%d.%m.%Y') AS purchaseDate,
date_format(date_add(from_unixtime(purchased), INTERVAL 5 YEAR), '%d.%m.%Y') AS warrantyEnd,
(
SELECT
COUNT(product)
FROM
productWarranty
) AS count
FROM
productWarranty s
GROUP BY
product

我不知道如何编写子查询,因为我需要使用类似 WHERE product = Hammer 的东西。但是我怎样才能让 sql 为每个产品循环呢?

最佳答案

您需要处理的是保修是否仍然有效或已过期以及保修日期,因此您首先需要构建一个表来反射(reflect)这一点:

select   product
, IF( warranty >= NOW(), 1, 0 ) as valid
, IF( warranty < NOW(), 1, 0 ) as expired
, warranty as last
from (
select product
, ADDDATE( purchased, INTERVAL 5 YEAR ) as warranty
from productWarranty
) w
group by product
;

那会让你得到类似的东西:

+---------+-------+---------+---------------------+
| product | valid | expired | warranty |
+---------+-------+---------+---------------------+
| Hammer | 1 | 0 | 2017-01-01 00:00:00 |
| Nipper | 1 | 0 | 2017-01-01 00:00:00 |
| Nipper | 1 | 0 | 2017-01-01 00:00:00 |
| Nipper | 1 | 0 | 2017-01-01 00:00:00 |
| Saw | 1 | 0 | 2017-01-01 00:00:00 |
| Saw | 0 | 1 | 2011-01-01 00:00:00 |
| Saw | 1 | 0 | 2017-01-01 00:00:00 |
| Saw | 1 | 0 | 2017-01-01 00:00:00 |
+---------+-------+---------+---------------------+

然后使用聚合函数过滤和汇总您要查找的信息:

select   product
, SUM( IF( warranty >= NOW(), 1, 0 ) ) as valid
, SUM( IF( warranty < NOW(), 1, 0 ) ) as expired
, MAX( warranty ) as last
from (
select product
, adddate( purchased, interval 5 year ) as warranty
from productWarranty
) w
group by product
;
+---------+-------+---------+---------------------+
| product | valid | expired | last |
+---------+-------+---------+---------------------+
| Hammer | 1 | 0 | 2017-01-01 00:00:00 |
| Nipper | 3 | 0 | 2017-01-01 00:00:00 |
| Saw | 3 | 1 | 2017-01-01 00:00:00 |
+---------+-------+---------+---------------------+

关于php - mysql用循环计数行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19496642/

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