子查询在mysql中返回多于1行

发布于 2024-10-25 00:25:37 字数 632 浏览 2 评论 0原文

我正在执行以下查询并收到错误“子查询返回超过 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

感谢您的帮助。 欧麦尔

I am executing the following query and got the error "Subquery returns more than 1 row"

My Query is

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

Thanks for your help.
Umar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_失温 2024-11-01 00:25:37

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

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”)

You can fix this error by limiting the subquery to return only one row, like this:

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

(Added 'LIMIT 1')

放飞的风筝 2024-11-01 00:25:37

好吧,发生这种情况是因为您的子查询返回了不止一行

zerkms 来救援

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

Well, this happens because your subquery returns more than one row

zerkms to the rescue

ps: it is really A LOT of similar questions in the right "related" bar.

千柳 2024-11-01 00:25:37

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

One approach is to add LIMIT 1 to your subquery. Another is to figure out if your subquery should logically be able to return more than one row and, if not, to fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文