添加INNER JOIN以将ID转换为另一个表中的值

时间:2013-10-23 14:09:19

标签: mysql sql

我有一个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,但我不知道如何扩展我的查询。所有trys都会产生一个空表。

这是我的查询:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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