3

我有一个生成以下输出的 SQL 脚本:

+------------+------------+--------------------+----------------------+---------------------+
| CUSTOMERID | TOTALCOUNT | VALIDWARRANTYCOUNT | EXPIREDWARRANTYCOUNT | LASTPURCHASED       |
+------------+------------+--------------------+----------------------+---------------------+
| 1          | 5          | 5                  | 0                    | 2013-12-24 14:37:45 |
| 2          | 3          | 3                  | 0                    | 2013-12-24 14:37:45 |
| 3          | 6          | 6                  | 0                    | 2013-10-23 13:37:45 |
+------------+------------+--------------------+----------------------+---------------------+

我想在附加列中查看客户的公司名称。我有一个额外的表,customer idcompany name我不知道如何扩展我的查询。所有尝试都会导致一个空表。

这是我的查询:

SELECT  p2c.customerid
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS TotalCount
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
        ) AS ValidWarrantyCount
    ,   (    
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
        ) AS ExpiredWarrantyCount
    ,   (
            SELECT MAX( from_unixtime(purchased) )
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS LastPurchased
FROM    (
            SELECT DISTINCT
                p2c.customerid
            FROM
                products2customers p2c
            INNER JOIN
                products p
            ON
                p2c.customerid = p.id
        ) AS p2c

我用没有连接的普通查询做了一个 SQL Fiddle,我希望有人能给我一个提示。

SQLFiddle

4

2 回答 2

1

只需JOIN在最后一行添加您的,然后在客户 ID 后选择公司:

SELECT  p2c.customerid,
        c.company
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS TotalCount
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
        ) AS ValidWarrantyCount
    ,   (    
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
        ) AS ExpiredWarrantyCount
    ,   (
            SELECT MAX( from_unixtime(purchased) )
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS LastPurchased
FROM    (
            SELECT DISTINCT
                p2c.customerid
            FROM
                products2customers p2c
        ) AS p2c
JOIN customers c ON c.id = p2c.customerid; <--

更新小提琴: http ://sqlfiddle.com/#!2/60396/5/0

于 2013-10-23T14:15:29.927 回答
1

只需将另一个联接添加到客户表中,如下所示

SELECT  p2c.customerid, company
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS TotalCount
    ,   (
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
        ) AS ValidWarrantyCount
    ,   (    
            SELECT COUNT(*)
            FROM   products2customers
            WHERE  customerid = p2c.customerid
               AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
        ) AS ExpiredWarrantyCount
    ,   (
            SELECT MAX( from_unixtime(purchased) )
            FROM   products2customers
            WHERE  customerid = p2c.customerid
        ) AS LastPurchased
FROM    (
            SELECT 
                p2c.customerid, c.company
            FROM
                products2customers p2c
            INNER JOIN
                products p
            ON
                p2c.customerid = p.id

inner join customers c 
on c.id=p2c.customerid group by 1,2
        ) AS p2c
于 2013-10-23T14:16:10.203 回答