“foo” 是什么意思?在这个 SQL Server 查询中是什么意思?

发布于 2024-09-03 09:24:54 字数 299 浏览 2 评论 0原文

例如...

SELECT * 
  FROM ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking,
                stud_id, 
                stud_name, 
                stud_mark 
           FROM tbl_student ) AS foo 
 WHERE ranking = 10

这里存在 foo...实际上它是做什么的?...

for eg...

SELECT * 
  FROM ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking,
                stud_id, 
                stud_name, 
                stud_mark 
           FROM tbl_student ) AS foo 
 WHERE ranking = 10

Here foo is present...actually what it does ?..

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

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

发布评论

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

评论(4

云巢 2024-09-10 09:24:54

在此示例中,foo 是表别名。使用方法如下:

SELECT foo.* 
  FROM ( SELECT RANK() OVER (ORDER BY ts.stud_mark DESC) AS ranking,
                ts.stud_id, 
                ts.stud_name, 
                ts.stud_mark 
           FROM tbl_student ts) AS foo 
 WHERE foo.ranking = 10

如果您没有为派生表(也称为内联视图)指定表别名,SQL Server(以及 MySQL)将抛出错误。

In this example, foo is a table alias. Here's how you'd use it:

SELECT foo.* 
  FROM ( SELECT RANK() OVER (ORDER BY ts.stud_mark DESC) AS ranking,
                ts.stud_id, 
                ts.stud_name, 
                ts.stud_mark 
           FROM tbl_student ts) AS foo 
 WHERE foo.ranking = 10

SQL Server (and MySQL for that matter) will throw an error if you do not specify a table alias for a derived table (AKA inline view).

月棠 2024-09-10 09:24:54

它只是一个别名。

别名可帮助您减少编写查询时可能需要输入的文本量。

例如:

SELECT customer.Name, customer.OpenDate FROM customer

可以缩短为:

SELECT c.Name, c.OpenDate FROM customer c

在您的示例中,它是一个派生表(不是物理表),现在您实际上可以说:

SELECT foo.someField 而不是 SELECT *

It is just an alias.

Aliases help you reduce the amount of text you may have to type out when writing queries.

For instance this:

SELECT customer.Name, customer.OpenDate FROM customer

Can be shortened to:

SELECT c.Name, c.OpenDate FROM customer c

In your example, it is a derived table (not a physical one) which now you can actually say:

SELECT foo.someField rather then SELECT *

深空失忆 2024-09-10 09:24:54

它是派生查询的表别名/标识符

如果没有它,您将收到错误,因为派生表没有标识符

SELECT * FROM 
 ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking, stud_id, stud_name, stud_mark FROM tbl_student )
WHERE ranking = 10

It's a table alias/identifier for the derived query

Without it, you'll get an error because the derived table has no identifier

SELECT * FROM 
 ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking, stud_id, stud_name, stud_mark FROM tbl_student )
WHERE ranking = 10
可爱咩 2024-09-10 09:24:54

最好问一下:SQL中“AS”后面的表达式是什么意思?

正如乔恩所说,这是一个别名。您可以使用它来代替 AS 之前的表达式。

SELECT veryverylongname AS V FROM dummy_table AS D WHERE D.V = 10

在这里您可以看到字段的两个别名 V 和表的别名 D。

您还可以使用它来为完整的 SELECT 语句添加别名,如示例中所示。然后,别名将表示子查询生成的表,并且将在 SELECT 关键字后面的子查询中指定字段。

It would be better to ask: What does the expresison after "AS" mean in SQL?

As Jon said, it's an alias. You can use it instead of the expression before the AS.

SELECT veryverylongname AS V FROM dummy_table AS D WHERE D.V = 10

Here you can see two aliases V for the field and D for the table.

You can also use it to alias complete SELECT-statements as in your example. The alias will then represent the table resulting from the sub-query, and will have the fields specifiy in the subquery after your SELECT-keyword.

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