mysql counting rows with loop

前端 未结 3 964
旧巷少年郎
旧巷少年郎 2021-01-25 07:31

I have the following table:

+-------------+--------------+ 
| product     | purchased    |
+-------------+--------------+ 
| Hammer      |   |
|         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-25 08:01

    Here's the answer I posted in your other question

    SELECT  p2c.pid AS productNumber,
            p.name AS productName,
            COUNT(*) AS registered,
            SUM(date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()) AS inWarranty,
            SUM(date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()) AS outOfWarranty,
            DATE_FORMAT( MAX( from_unixtime(purchased) ), '%d.%m.%Y') AS lastPurchased,
            DATE_FORMAT( date_add( MAX( from_unixtime(purchased) ), INTERVAL 5 YEAR), '%d.%m.%Y') AS warrantyUntil
    FROM products2customers p2c
    JOIN products p ON p.id = p2c.pid
    GROUP BY p2c.pid
    ORDER BY inWarranty DESC
    

提交回复
热议问题