在统计查询中定义别名
我需要这样做:
SELECT COUNT(*)
FROM some_table
WHERE someAlias1 = someValue1
AND someAlias2 = someValue2;
someAlias
是some_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行嵌套的 SELECT 语句,然后从内部查询中提取计数,我真的没有找到使用列名进行转义的方法
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
我无法想象无法直接命名列的情况。如果列名重复,则在前面添加表名:
如果列名是保留关键字或包含空格,则用引号引起来:
您甚至可以将两者结合起来:
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:
If the column name is a reserved keyword or contains spaces, quote it:
You can even combine both:
为什么需要使用别名?在查询中使用别名的唯一原因是在“having”子句中重复使用,例如:
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: