SQL语句中出现不熟悉的字符

发布于 2024-07-06 15:52:27 字数 401 浏览 13 评论 0原文

我认为这是 SQL 新手的问题,但这里是。

我有一个基于示例用户定义函数组合在一起的 SQL 查询(SQL Server 2005):

SELECT 
    CASEID,
    GetNoteText(CASEID)
FROM 
( 
    SELECT 
        CASEID 
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) i
GO 

UDF 工作得很好(它连接了相关表中多行的数据,如果这很重要),但我'我对 FROM 子句后面的“i”感到困惑。 使用 i 查询可以正常工作,但如果没有 i,则查询会失败。 “我”有什么意义?

编辑:正如乔尔在下面指出的,它不是关键字

This is sort of SQL newbie question, I think, but here goes.

I have a SQL Query (SQL Server 2005) that I've put together based on an example user-defined function:

SELECT 
    CASEID,
    GetNoteText(CASEID)
FROM 
( 
    SELECT 
        CASEID 
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) i
GO 

the UDF works great (it concatenates data from multiple rows in a related table, if that matters at all) but I'm confused about the "i" after the FROM clause. The query works fine with the i but fails without it. What is the significance of the "i"?

EDIT: As Joel noted below, it's not a keyword

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

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

发布评论

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

评论(7

青巷忧颜 2024-07-13 15:52:27

当您在 FROM 子句中使用子查询时,您需要为查询指定名称。 由于名称对您来说并不重要,因此通常会选择“i”或“a”等简单的名称。 但是您可以在那里放置任何您想要的名称 - “i”本身没有任何意义,而且它当然不是关键字。

如果您有一个非常复杂的查询,您可能需要将子查询与其他查询或表连接起来。 在这种情况下,名称就变得更加重要,您应该选择更有意义的名称。

When you use a subquery in the FROM clause, you need to give the query a name. Since the name doesn't really matter to you, something simple like 'i' or 'a' is often chosen. But you could put any name there you wanted- there's no significance to 'i' all by itself, and it's certainly not a keyword.

If you have a really complex query, you may need to join your sub query with other queries or tables. In that case the name becomes more important and you should choose something more meaningful.

开始看清了 2024-07-13 15:52:27

i 命名(子查询),这是必需的,也是进一步连接所需要的。

当连接表之间存在冲突的列名称时,您必须在外部查询中的列前加上子查询名称的前缀,例如:

SELECT 
    c.CASEID, c.CASE_NAME,
    a.COUNT AS ATTACHMENTSCOUNT, o.COUNT as OTHERCOUNT,
    dbo.GetNoteText(c.CASEID)
FROM CASES c
LEFT OUTER JOIN
( 
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) a
ON a.CASEID = c.CASEID
LEFT OUTER JOIN
(
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        OTHER
    GROUP BY 
        CASEID 
) o
ON o.CASEID = c.CASEID

The i names the (subquery), which is required, and also needed for further joins.

You will have to prefix columns in the outer query with the subquery name when there are conflicting column names between joined tables, like:

SELECT 
    c.CASEID, c.CASE_NAME,
    a.COUNT AS ATTACHMENTSCOUNT, o.COUNT as OTHERCOUNT,
    dbo.GetNoteText(c.CASEID)
FROM CASES c
LEFT OUTER JOIN
( 
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) a
ON a.CASEID = c.CASEID
LEFT OUTER JOIN
(
    SELECT 
        CASEID, COUNT(*) AS COUNT
    FROM 
        OTHER
    GROUP BY 
        CASEID 
) o
ON o.CASEID = c.CASEID
左耳近心 2024-07-13 15:52:27

“i”为您的 select 语句提供一个有效的表名称。 它也可以写成“AS i”(我想 - 我不是 MSSQLServer 人员)。

The "i" is giving your select statement an effective table name. It could also be written (I think - I'm not an MSSQLServer guy) as "AS i".

dawn曙光 2024-07-13 15:52:27

正如其他人所说,它是子查询的表名别名。

在子查询之外,您可以使用 i.CASEID 来引用子查询结果。

在这个例子中它并没有太大用处,但是当你有多个子查询时,它是一个非常重要的消歧工具。

不过,我会选择一个更好的变量名。 甚至“温度”也更好。

As others stated, it's a table name alias for the subquery.

Outside the subquery, you could use i.CASEID to reference into the subquery results.

It's not too useful in this example, but when you have multiple subqueries, it is a very important disambiguation tool.

Although, I'd choose a better variable name. Even "temp" is better.

如若梦似彩虹 2024-07-13 15:52:27

i 命名您的子查询,以便如果您有一个包含大量子查询的复杂查询,并且您需要访问字段,您可以以明确的方式执行此操作。

最好的做法是为子查询提供更具描述性的名称,以防止您在开始编写较长查询时感到困惑,没有什么比必须向上滚动较长的 sql 语句更糟糕的了,因为您忘记了哪个 i.id 是正确的一个或正在从中检索 c.name 的表/查询。

The i names your subquery so that if you have a complex query with numerous subqueries and you need to access the fields you can do so in an unambiguous way.

It is good practice to give your subqueries more descriptive names to prevent your own confusion when you start getting into writing longer queries, there is nothing worse then having to scroll back up through a long sql statement because you have forgotten which i.id is the right one or which table/query c.name is being retrieved from.

以为你会在 2024-07-13 15:52:27

吸取的教训是想想谁将继承你的代码。 正如其他人所说,如果代码是这样写的:

SELECT DT1.CASEID, GetNoteText(DT1.CASEID) 
FROM (
   SELECT CASEID 
   FROM ATTACHMENTS
   GROUP BY CASEID
) AS DT1 (CASEID);

那么读者理解它的可能性就会增加,甚至可能会发现“DT1”暗指“派生表”。

The lesson to learned is to think of the person who will inherit your code. As others have said, if the code had been written like this:

SELECT DT1.CASEID, GetNoteText(DT1.CASEID) 
FROM (
   SELECT CASEID 
   FROM ATTACHMENTS
   GROUP BY CASEID
) AS DT1 (CASEID);

then there's an increased chance the reader would have figured it out and may even pick up on 'DT1' alluding to a 'derived table'.

弄潮 2024-07-13 15:52:27

“派生表”是一个技术术语,表示在 FROM 子句中使用子查询。

SQL Server 联机丛书语法表明 table_alias 在这种情况下不是可选的; “table_alias”不包含在括号中,根据 Transact-SQL 语法约定,括号中的内容是可选的。 关键字“AS”是可选的,因为它括在方括号中...

   衍生表 [ AS ] 表别名 [ ( 列别名 [ ,...n ] ) ]

FROM (Transact -SQL):
http://msdn.microsoft.com/en-us /library/ms177634(SQL.90).aspx

Transact-SQL 语法约定:
http://msdn.microsoft.com/en-us /library/ms177563(SQL.90).aspx

"Derived table" is a technical term for using a subquery in the FROM clause.

The SQL Server Books Online syntax shows that table_alias is not optional in this case; "table_alias" is not enclosed in brackets and according to the Transact-SQL Syntax Conventions, things in brackets are optional. The keyword "AS" is optional though since it is enclosed in brackets...

   derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ]

FROM (Transact-SQL):
http://msdn.microsoft.com/en-us/library/ms177634(SQL.90).aspx

Transact-SQL Syntax Conventions:
http://msdn.microsoft.com/en-us/library/ms177563(SQL.90).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文