选择满足2个条件的计数?

发布于 2024-10-02 17:44:56 字数 399 浏览 8 评论 0原文

Select count(*) from Merchant where Email in 
(Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn>  (Select TimeSent from MerchantInvitations where MerchantID = '16092')

我想要满足其电子邮件位于 MerchantInvitations 的 SentEmail 列中的商家的计数 和 CreatedOn >商户邀请中发送的时间。但我收到一个错误“子查询返回超过 1 个值。当子查询时这是允许的 遵循 =、!=、<、<= 等,或者当子查询用作表达式时“请帮帮我!提前致谢!

Select count(*) from Merchant where Email in 
(Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn>  (Select TimeSent from MerchantInvitations where MerchantID = '16092')

I want the count for Merchants satisfying whose email is in SentEmail column in MerchantInvitations
and CreatedOn > Timesent in MerchantInvitations. But i get an error that "Subquery returned more than 1 value. This is permitted when the subquery
follows =,!=,<,<=, etc or when the subquery is used as an expression" Help me out please!! Thanks in advance!

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

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

发布评论

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

评论(6

半葬歌 2024-10-09 17:44:56

MerchantInvitations 中 MerchantID = '16092' 的多个条目会导致此问题。也许您想要最新的 TimeSent,如下所示:

Select count(*) from Merchant where Email in 
(Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn>  (Select MAX(TimeSent) from MerchantInvitations where MerchantID = '16092')

More than one entry in MerchantInvitations where MerchantID = '16092' would cause this problem. Maybe you want the most recent TimeSent, like this:

Select count(*) from Merchant where Email in 
(Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn>  (Select MAX(TimeSent) from MerchantInvitations where MerchantID = '16092')
迷你仙 2024-10-09 17:44:56

如果您的第二个子查询返回多于一行,它将永远不会工作。不过,您可以使用 ANY 或 ALL 关键字:

SELECT COUNT(*)
FROM Merchant
WHERE Email IN (SELECT SentEmail
               FROM MerchantInvitations
               WHERE MerchantID = '16092') -- Why not 16092? Is it really a string?
AND CreatedOn > ANY (SELECT TimeSent
                     FROM MerchantInvitations
                     WHERE MerchantID = '16092');

但看起来您只想做一个简单的连接:

SELECT COUNT(*)
FROM Merchant M
INNER JOIN MerchantInvitations I
    ON M.MerchantID = I.MerchantID -- You may have to change this
    AND M.Email = I.SentEmail
    AND M.CreatedOn > I.TimeSent
WHERE M.MerchantID = 16092;

这绝对应该比子查询更快。

If your second subquery returns more than one row, it will never work. You can use ANY or ALL keyword, though :

SELECT COUNT(*)
FROM Merchant
WHERE Email IN (SELECT SentEmail
               FROM MerchantInvitations
               WHERE MerchantID = '16092') -- Why not 16092? Is it really a string?
AND CreatedOn > ANY (SELECT TimeSent
                     FROM MerchantInvitations
                     WHERE MerchantID = '16092');

But it looks like you just want to do a simple join :

SELECT COUNT(*)
FROM Merchant M
INNER JOIN MerchantInvitations I
    ON M.MerchantID = I.MerchantID -- You may have to change this
    AND M.Email = I.SentEmail
    AND M.CreatedOn > I.TimeSent
WHERE M.MerchantID = 16092;

Which should definitely be faster than your subqueries.

月隐月明月朦胧 2024-10-09 17:44:56

看起来您的子查询返回了多个 TimeSent ,与 CreatedOn 进行了比较。要解决此问题,请添加 ALL 说明符:

Select count(*) from Merchant where 
Email in (Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn > ALL (Select TimeSent from MerchantInvitations where MerchantID = '16092')

Looks like you sub-query is returning more than one TimeSent which is compared with CreatedOn. To fix this add an ALL specifier as:

Select count(*) from Merchant where 
Email in (Select SentEmail from MerchantInvitations where MerchantID = '16092') AND 
CreatedOn > ALL (Select TimeSent from MerchantInvitations where MerchantID = '16092')
沦落红尘 2024-10-09 17:44:56

问题可能是

 (Select TimeSent from MerchantInvitations where MerchantID = '16092')

如果该子查询返回多个值,那么数据库引擎如何知道要与 CreatedOn 进行比较?

您可以通过在子查询中使用 MIN(TimeSent) 或 MAX(TimeSent)(代替 TimeSent)来解决该问题(以合适的为准)。

The problem is probably

 (Select TimeSent from MerchantInvitations where MerchantID = '16092')

If that sub-query returns more than one value, then how does the database engine know which to compare against CreatedOn?

You can fix the problem by using either MIN(TimeSent) or MAX(TimeSent) in your sub-query (in place of TimeSent), whichever is appropriate.

泅人 2024-10-09 17:44:56

使用 where 存在而不是 in

Select count(*) from Merchant where exists
(Select SentEmail from MerchantInvitations where MerchantID = '16092' and MerchantInvitations.sentemail = merchant.email) AND 
CreatedOn>  (Select TimeSent from MerchantInvitations where MerchantID = '16092' and MerchantInvitations.sentemail = merchant.email)

use where exists instead of in

Select count(*) from Merchant where exists
(Select SentEmail from MerchantInvitations where MerchantID = '16092' and MerchantInvitations.sentemail = merchant.email) AND 
CreatedOn>  (Select TimeSent from MerchantInvitations where MerchantID = '16092' and MerchantInvitations.sentemail = merchant.email)
说谎友 2024-10-09 17:44:56

使用单个 EXISTS 子查询:

Select count(*) 
from Merchant M
where exists
(select null
 from MerchantInvitations I
 where M.Email = I.SentEmail AND 
       M.CreatedOn > I.TimeSent AND
       I.MerchantID = '16092')

Use a single EXISTS subquery:

Select count(*) 
from Merchant M
where exists
(select null
 from MerchantInvitations I
 where M.Email = I.SentEmail AND 
       M.CreatedOn > I.TimeSent AND
       I.MerchantID = '16092')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文