首页 > 解决方案 > 使用不存在的列时嵌套的 SELECT“有效” - 为什么?

问题描述

伙计们,我在 SQLite 中有以下查询:

select license, username from check_table where
(
    username not in (
        select username from Address
    )
) order by license, username;

Address是另一张桌子。有趣的部分是:Address没有username专栏!

细节:

现在,我的问题是:为什么我在这里没有收到错误?!这是否与我从未得到任何结果的事实有关?

标签: sqlsqlite

解决方案


username从 中选择check_table,而不是从address表中选择。

尝试添加别名并检查一下:

select ct.license, ct.username 
from check_table as ct
where
(
    ct.username not in (
        select ct.username 
        from Address as a
    )
) order by ct.license, ct.username;

我敢打赌,如果您尝试使用select a.username...,您会收到关于不存在列的错误。

为此,当您在查询中使用多个表时,最好使用别名。


推荐阅读