2011-03-22 190 views
1

我執行下面的查詢超過1行,並得到了錯誤「子查詢返回多個1行」子查詢返回在MySQL

我的查詢是

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 

感謝您的幫助。 歐麥爾

回答

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 

(增加了「LIMIT 1」)

0

好了,這是因爲你的子查詢返回不止一行

zerkms救援

PS:這是真的的「相關」欄右側類似的問題很多

0

一種方法是增加LIMIT 1到您的子查詢。另一個是確定你的子查詢是否應該能夠返回多於一行,如果不是,則修復它。