在统计查询中定义别名

发布于 2024-11-30 04:31:14 字数 439 浏览 0 评论 0原文

我需要这样做:

SELECT COUNT(*)
  FROM some_table
 WHERE someAlias1 = someValue1 
   AND someAlias2 = someValue2;

someAliassome_table 中列的别名。就我而言,我无法直接命名列;我需要使用别名。

问题是我只知道在 select 子句中定义别​​名,在这种情况下我不知道该怎么做。

在这种情况下有办法完成我所需要的吗?

编辑:为什么我需要别名?我正在从替代部分构建查询,上面的条件适用于不同表中的不同列,但具有相同的逻辑角色。因此,我需要一种方法来关联具有相同名称的不同替代列。

如果您仅在知道答案的情况下回答这个问题,即使您不明白为什么我需要别名,我将不胜感激

I need to do:

SELECT COUNT(*)
  FROM some_table
 WHERE someAlias1 = someValue1 
   AND someAlias2 = someValue2;

someAlias is an alias for a column in some_table. In my case I can't name the columns directly; I need to use aliases.

The issue is that I only know about defining aliases inside the select clause which I don't see how I can do in this case.

Is there a way to accomplish what I need in this case?

edit: Why do I need aliases? I'm building a query from alternative parts, and the condition above applies to different columns from different tables, but with the same logical role. So I need a way to relate to different alternative columns with the same name.

I will appreciate if you answer this question only if you know an answer, even if you don't understand why may I need an alias

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

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

发布评论

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

评论(3

久伴你 2024-12-07 04:31:14

您可以执行嵌套的 SELECT 语句,然后从内部查询中提取计数,我真的没有找到使用列名进行转义的方法

SELECT COUNT(*) 
  FROM(
        SELECT col1 as someAlias1, 
               col2 as someAlias2
        FROM   some_table
        WHERE  someAlias1 = someValue1
        AND    someAlias2 = someValue2
       ) as inner

You could do a nested SELECT statement then draw the count out from the inner query, I don't really see a way to escape using the column names

SELECT COUNT(*) 
  FROM(
        SELECT col1 as someAlias1, 
               col2 as someAlias2
        FROM   some_table
        WHERE  someAlias1 = someValue1
        AND    someAlias2 = someValue2
       ) as inner
芸娘子的小脾气 2024-12-07 04:31:14

我无法想象无法直接命名列的情况。如果列名重复,则在前面添加表名:

WHERE someTable1.someColumn1 = someValue1

如果列名是保留关键字或包含空格,则用引号引起来:

WHERE `some Column1` = someValue1

您甚至可以将两者结合起来:

WHERE someTable1.`some Column1` = someValue1

I can't figure out a scenario where you can't name the columns directly. If the column name is duplicated, prepend the table name:

WHERE someTable1.someColumn1 = someValue1

If the column name is a reserved keyword or contains spaces, quote it:

WHERE `some Column1` = someValue1

You can even combine both:

WHERE someTable1.`some Column1` = someValue1
灼痛 2024-12-07 04:31:14

为什么需要使用别名?在查询中使用别名的唯一原因是在“having”子句中重复使用,例如:

select count(*)  as C 
  from some_table 
 where someAlias1=someValue1 
   and someAlias2=someValue2
having C > someLimit1;

Why do you need to use aliases? The only reason to use aliases in your query would be for re-use in e.g. an "having" clause, like:

select count(*)  as C 
  from some_table 
 where someAlias1=someValue1 
   and someAlias2=someValue2
having C > someLimit1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文