选择满足2个条件的计数?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MerchantInvitations 中 MerchantID = '16092' 的多个条目会导致此问题。也许您想要最新的 TimeSent,如下所示:
More than one entry in MerchantInvitations where MerchantID = '16092' would cause this problem. Maybe you want the most recent TimeSent, like this:
如果您的第二个子查询返回多于一行,它将永远不会工作。不过,您可以使用 ANY 或 ALL 关键字:
但看起来您只想做一个简单的连接:
这绝对应该比子查询更快。
If your second subquery returns more than one row, it will never work. You can use ANY or ALL keyword, though :
But it looks like you just want to do a simple join :
Which should definitely be faster than your subqueries.
看起来您的子查询返回了多个
TimeSent
,与CreatedOn
进行了比较。要解决此问题,请添加ALL
说明符:Looks like you sub-query is returning more than one
TimeSent
which is compared withCreatedOn
. To fix this add anALL
specifier as:问题可能是
如果该子查询返回多个值,那么数据库引擎如何知道要与 CreatedOn 进行比较?
您可以通过在子查询中使用 MIN(TimeSent) 或 MAX(TimeSent)(代替 TimeSent)来解决该问题(以合适的为准)。
The problem is probably
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.
使用 where 存在而不是 in
use where exists instead of in
使用单个 EXISTS 子查询:
Use a single EXISTS subquery: