1

这是查询:

    SELECT count(a.id) as itemCount, 
           concat(a.firstname, ' ', a.lastname) as name, 
           oi.status, a.id as adminID, 
           (SELECT count(a.id) as oldItemsCount 
                   FROM bl_orderitems oi, 
                        bl_researcher r,             
                        tbladmins a 
                   WHERE oi.status='Pending' AND 
                         r.id=oi.researcherid AND 
                         r.researchermanagerid=a.id AND
                         DATE(oi.regdate)=DATE_SUB(CURDATE(), INTERVAL 3 DAY))
                   as oldItemCount
    FROM bl_orderitems oi, 
           bl_researcher r,
           tbladmins a 
    WHERE oi.status in ('Pending', 'QA') AND
           r.id=oi.researcherid AND 
           r.researchermanagerid=a.id 
    GROUP BY 
          name, adminID, oi.status;

此查询应返回如下内容:

 itemCount    name            status    adminID  oldItemCount 
    5         John McKay      Pending      20        125
    15        Clown Ho        QA           12        125
    39        Jack Fell       Pending      13        125
    5         Mark Grunigen   Pending       2        125
    1         Kim Jun Li      QA            2        125
    206       Abama Oladen    Pending       4        125

虽然我试图实现它返回唯一的 oldItemCount。

oldItemCount 应该是该套件的 count(a.id) " DATE(oi.regdate)=DATE_SUB(CURDATE(), INTERVAL 3 DAY))"

如果我在子查询上尝试使用 DISTINCT,它会说Operand should contain 1 column(s)

4

1 回答 1

3

您正在寻找的是COUNT(DISTINCT)

编辑:根据您的评论,您应该尝试这样的事情:

SELECT count(DISTINCT oi.id) as itemCount, 
       concat(a.firstname, ' ', a.lastname) as name, 
       oi.status, a.id as adminID, 
       (SELECT count(oi2.id) as oldItemsCount 
               FROM bl_orderitems oi2, 
                    bl_researcher r2
               WHERE oi2.status='Pending' AND 
                     r2.id=oi.researcherid AND 
                     r2.researchermanagerid=a.id AND
                     DATE(oi2.regdate)=DATE_SUB(CURDATE(), INTERVAL 3 DAY))
               as oldItemCount
FROM bl_orderitems oi, 
       bl_researcher r,
       tbladmins a 
WHERE oi.status in ('Pending', 'QA') AND
       r.id=oi.researcherid AND 
       r.researchermanagerid=a.id 
GROUP BY 
      name, adminID, oi.status;

它更简单,可能适合您的需求。

于 2013-05-27T22:09:33.060 回答