1

我正在执行以下查询并收到错误“子查询返回超过 1 行”

我的查询是

SELECT pkTicketID,
       TicketReplyDateAdded,
       TIMESTAMPDIFF(DAY, TicketReplyDateAdded, now()) as NumberOfDays
  FROM tickets as T
    LEFT JOIN ticket_replies as TR ON T.fkTicketReplyID=TR.pkTicketReplyID
  WHERE 1 AND T.fkEmployeeID = '4'
    AND (SELECT TIMESTAMPDIFF(DAY, TicketReplyDateAdded, now()) as NumberOfDays
           FROM tickets as T
             LEFT JOIN ticket_replies as TR
               ON T.fkTicketReplyID=TR.pkTicketReplyID
        ) = 7
    AND T.TicketStatus = 'Replied'
  ORDER BY pkTicketReplyID DESC

谢谢你的帮助。奥马尔

4

3 回答 3

2

您可以通过将子查询限制为仅返回一行来修复此错误,如下所示:

SELECT pkTicketID,TicketReplyDateAdded,
       TIMESTAMPDIFF(DAY, TicketReplyDateAdded,now()) as NumberOfDays 
FROM   tickets as T 
       LEFT JOIN ticket_replies as TR
       ON T.fkTicketReplyID=TR.pkTicketReplyID 
WHERE  1 
AND    T.fkEmployeeID = '4' 
AND    (SELECT TIMESTAMPDIFF(DAY, TicketReplyDateAdded, now()) as NumberOfDays 
        FROM tickets as T LEFT JOIN ticket_replies as TR 
        ON T.fkTicketReplyID=TR.pkTicketReplyID
        LIMIT 1) = 7 
AND     T.TicketStatus = 'Replied' 
ORDER   BY pkTicketReplyID DESC

(添加了“限制 1”)

于 2011-03-22T07:04:49.457 回答
0

好吧,这是因为您的子查询返回不止一行

zerkms 救援

ps:在右侧的“相关”栏中确实有很多类似的问题。

于 2011-03-22T06:54:05.427 回答
0

一种方法是添加LIMIT 1到您的子查询中。另一个是确定您的子查询在逻辑上是否应该能够返回多行,如果不能,则修复它。

于 2011-03-22T07:05:32.957 回答