我的 MYSQL 查询出了什么问题?
SELECT
( SELECT
SUM(IF(status = 'Active', 1, 0)) AS `univ_active`,
SUM(IF(status = 'Inactive', 1, 0)) AS 'univ_inactive',
Count(*)
FROM online_university
)
AS tot_university,
( SELECT
SUM(IF(status = 'Active', 1,0)) AS `user_active`,
SUM(IF(status = 'Inactive', 1,0)) AS 'user_inactive'
Count(*)
FROM online_register_user)
AS tot_users
结果必须是
univ_active=4 univ_inactive=2 tot_university=6
user_active=10 user_inactive=3 tot_users = 13
我怎样才能得到这个?上面的查询返回错误:操作数应包含 1 列
这用于从返回表中活动、非活动、总计记录的所有表中为项目准备报告。如果这个方法是错误的那么我应该使用什么?任何建议。
SELECT
( SELECT
SUM(IF(status = 'Active', 1, 0)) AS `univ_active`,
SUM(IF(status = 'Inactive', 1, 0)) AS 'univ_inactive',
Count(*)
FROM online_university
)
AS tot_university,
( SELECT
SUM(IF(status = 'Active', 1,0)) AS `user_active`,
SUM(IF(status = 'Inactive', 1,0)) AS 'user_inactive'
Count(*)
FROM online_register_user)
AS tot_users
Result must be
univ_active=4 univ_inactive=2 tot_university=6
user_active=10 user_inactive=3 tot_users = 13
How can i get this? The above query returning ERROR: Operand should contain 1 column(s)
This to prepare report for a project from all tables returning Active, Inactive, Total records from the table. If this method is wrong then what shall i user? Any suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
子查询只能返回一列。您要么需要执行多个子查询、联接,要么需要执行廉价的 concat hack (
CONCAT_WS(',', SUM(IF(status = 'Active', 1,0)), SUM(IF(status = 'Inactive) ', 1,0))
) 在单个子查询中。Subqueries can only return one column. You either need to do multiple subqueries, a join, or a cheap concat hack (
CONCAT_WS(',', SUM(IF(status = 'Active', 1,0)), SUM(IF(status = 'Inactive', 1,0))
) in a single subquery.正如错误所示,您正在选择一个返回两列的子查询。
SQL 中不存在。您必须将每个子查询放在自己的位置:
但是,在单个查询中完成所有这些操作实际上没有任何好处。说起来容易得多:
然后将这些结果一起呈现在应用程序层中。 WHERE 子句速度更快,并且可以使用 SUM/IF 等计算表达式无法使用的适当索引。
更简单的是:
As the error says, you're SELECTing a subquery that returns two columns.
doesn't exist in SQL. You would have to put each subquery on its own:
However, there is really no benefit to doing all this in a single query. Much easier to say:
then present those results together in the application layer. A WHERE clause is faster and can use proper indexes which a calculated expression like SUM/IF cannot.
Simpler still:
正如 BipedalShark 所说,您的查询应该有 1 列,而现在有 2 列。但除此之外,您应该考虑使用 count(*) 和 where 子句。所以它应该是这样的:
As BipedalShark said, your queries should have 1 column and they have 2 now. But besides that you should think of using count(*) and where clause. So it should be smth like this:
通过像我刚刚在问题中所做的那样布局查询,很明显别名 tot_university 会与两列相关联,这是不可能的......
除了这个语法/逻辑错误之外,整个查询看起来很糟糕结构化以产生所需的结果。
它看起来非常像家庭作业或自布置的学习,所以我不会用现成的查询来破坏它,而是这里有一些提示。
By layout the query as I just did in the question, it becomes very obvious that the alias tot_university, for example would be associated with two columns, which is not possible...
Aside from this syntax/logic error, the whole query seems poorly structured to produce the desired result.
It looks very much like homework, or self assigned learning, so I won't spoil it with a ready-made query, instead here are a few hints.