简体   繁体   English

SQL INNER JOIN看不到表别名

[英]SQL INNER JOIN does not see table alias

I'm having trouble with this SQL statement: 我在处理此SQL语句时遇到了麻烦:

SELECT linkstable.date, (linkstable.count - x.count) 
FROM (SELECT .....) AS linkstable 
INNER JOIN linkstable AS x 
  ON linkstable.date = x.date+1 

It tells me that links table cannot be found although (SQL error 1146) there clearly is an alias to that select query. 它告诉我,尽管(SQL错误1146)显然没有该选择查询的别名,但找不到链接表。 Can anybody tell me please how do I bypass this error? 有人可以告诉我如何绕过此错误吗?

You cannot alias an alias. 您不能为别名加上别名。 If you want to join table to itself, repeat the select: 如果要将表自身连接,请重复选择:

SELECT linkstable.date, (linkstable.count - x.count) 
FROM (SELECT .....) AS linkstable 
INNER JOIN (SELECT .....) AS x ON linkstable.date = x.date+1

If inner select is too cumbersome or you're running into performance issues insert its results into a temp table and use it in your main query. 如果内部选择太麻烦,或者您遇到性能问题,请将其结果插入到临时表中,然后在主查询中使用它。

You can't reference a subquery's alias in the FROM clause like that. 您不能像这样在FROM子句中引用子查询的别名。 However, if your database supports Common Table Expressions, you can use the WITH clause to accomplish the same thing: 但是,如果您的数据库支持通用表表达式,则可以使用WITH子句完成相同的操作:

WITH linkstable as (SELECT ...)
SELECT linkstable.date, (linkstable.count - x.count) 
FROM linkstable 
INNER JOIN linkstable AS x 
  ON linkstable.date = x.date+1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM