我的 MYSQL 查询出了什么问题?

发布于 2024-08-12 10:58:50 字数 661 浏览 6 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

清风挽心 2024-08-19 10:58:50

子查询只能返回一列。您要么需要执行多个子查询、联接,要么需要执行廉价的 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.

扶醉桌前 2024-08-19 10:58:50

正如错误所示,您正在选择一个返回两列的子查询。

SELECT (one_thing, another_thing) AS combined_thing

SQL 中不存在。您必须将每个子查询放在自己的位置:

SELECT (
    SELECT SUM(IF(status='Active', 1, 0)) FROM online_university
) AS univ_active, (
    SELECT SUM(IF(status='Inactive', 1, 0)) FROM online_university
) AS univ_inactive, (
    SELECT SUM(IF(status='Active' OR status='Inactive', 1, 0)) FROM online_university
) AS tot_university, (
    SELECT SUM(IF(status='Active', 1, 0)) FROM online_register_user
) AS user_active, (
    -- and so on

但是,在单个查询中完成所有这些操作实际上没有任何好处。说起来容易得多:

SELECT COUNT(*) FROM online_university WHERE status='Active';  -- univ_active
SELECT COUNT(*) FROM online_university WHERE status='Inactive';  -- univ_inactive
SELECT COUNT(*) FROM online_university;  -- tot_university
SELECT COUNT(*) FROM online_register_user WHERE status='Active';  -- user_active
    -- and so on

然后将这些结果一起呈现在应用程序层中。 WHERE 子句速度更快,并且可以使用 SUM/IF 等计算表达式无法使用的适当索引。

更简单的是:

SELECT status, COUNT(*) FROM online_university GROUP BY status;
SELECT status, COUNT(*) FROM online_register_user GROUP BY status;

As the error says, you're SELECTing a subquery that returns two columns.

SELECT (one_thing, another_thing) AS combined_thing

doesn't exist in SQL. You would have to put each subquery on its own:

SELECT (
    SELECT SUM(IF(status='Active', 1, 0)) FROM online_university
) AS univ_active, (
    SELECT SUM(IF(status='Inactive', 1, 0)) FROM online_university
) AS univ_inactive, (
    SELECT SUM(IF(status='Active' OR status='Inactive', 1, 0)) FROM online_university
) AS tot_university, (
    SELECT SUM(IF(status='Active', 1, 0)) FROM online_register_user
) AS user_active, (
    -- and so on

However, there is really no benefit to doing all this in a single query. Much easier to say:

SELECT COUNT(*) FROM online_university WHERE status='Active';  -- univ_active
SELECT COUNT(*) FROM online_university WHERE status='Inactive';  -- univ_inactive
SELECT COUNT(*) FROM online_university;  -- tot_university
SELECT COUNT(*) FROM online_register_user WHERE status='Active';  -- user_active
    -- and so on

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:

SELECT status, COUNT(*) FROM online_university GROUP BY status;
SELECT status, COUNT(*) FROM online_register_user GROUP BY status;
心意如水 2024-08-19 10:58:50

正如 BipedalShark 所说,您的查询应该有 1 列,而现在有 2 列。但除此之外,您应该考虑使用 count(*) 和 where 子句。所以它应该是这样的:

select
 (select count(*) from online_university where status = 'Active') as univ_active,
 (select count(*) from online_university where status = 'Inactive') as univ_inactive,
 (select count(*) from online_register_user where status = 'Active') as user_active,
 (select count(*) from online_register_user where status = 'Active') as user_inactive

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:

select
 (select count(*) from online_university where status = 'Active') as univ_active,
 (select count(*) from online_university where status = 'Inactive') as univ_inactive,
 (select count(*) from online_register_user where status = 'Active') as user_active,
 (select count(*) from online_register_user where status = 'Active') as user_inactive
贪了杯 2024-08-19 10:58:50

通过像我刚刚在问题中所做的那样布局查询,很明显别名 tot_university 会与两列相关联,这是不可能的......

除了这个语法/逻辑错误之外,整个查询看起来很糟糕结构化以产生所需的结果。

它看起来非常像家庭作业或自布置的学习,所以我不会用现成的查询来破坏它,而是这里有一些提示。

  • 信息来自两个不同的、不相关的表,也许使用 UNION 在单个查询中获取结果(同时有效地运行两个查询)(然后您可以使用带有一些文本(例如“Univ”、“Public”)的额外列来区分这两个查询行)
  • SUM(IF column = x, 1,0) 是对特定值进行计数的好技巧,而无需进行分组依据,本质上是一步“汇总”计数。
  • 如果您只关注文本类型结果(例如按原样写入报告),则 concat() 技巧很好,否则,最好将结果保留在单独的列中,以便进一步处理,显示在桌子等...

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.

  • the information comes from two distinct, unrelated tables, maybe use UNION to obtain results in a single query (while effectively running two queries) (you would then use an extra column with some text like 'Univ', 'Public' to differentiate the two lines)
  • the SUM(IF column = x, 1,0) is a good trick to count particular values, without having to do a group by, essentially "rolling up" the counts in one step.
  • the concat() trick is good if your focus is exclusively on textual type results, for example to write, as-is, in a report, otherwise, it would be preferable to keep the results in separate columns, for further processing, display in tables etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文