sql - 使用不存在的列时嵌套的 SELECT“有效” - 为什么?
问题描述
伙计们,我在 SQLite 中有以下查询:
select license, username from check_table where
(
username not in (
select username from Address
)
) order by license, username;
Address
是另一张桌子。有趣的部分是:Address
没有username
专栏!
细节:
- 结果:
Query finished in 0.004 second(s)
- 如果我修改
username
零件(例如修改为userrname
),我会收到一个no such column
错误,这完全没问题 - 它永远不会返回任何结果,即使我在子选择中替换
username
为(实际存在) - 这完全奇怪,因为它确实应该。mail_username
现在,我的问题是:为什么我在这里没有收到错误?!这是否与我从未得到任何结果的事实有关?
解决方案
您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...
,您会收到关于不存在列的错误。
为此,当您在查询中使用多个表时,最好使用别名。
推荐阅读
- flyte - 是否有从字符串创建 blob 类型的示例?
- python - 熊猫:获取独特的元素然后合并
- javascript - document.addEventListener 不在 JEST 的覆盖范围内
- typescript - 如何在本机反应中解锁一个屏幕上的旋转
- javascript - 我无法在主机上启动 node.js
- xamarin.forms - 如何在 Xamarin Forms UWP 上设置深色主题?
- python - 使用单张图像张量流进行预测,keras
- python - 使用 python 连接到远程 mongodb 在我的本地主机上不起作用
- react-native - react-native - 如何使redux在每次使用标签导航更改页面时都不更新
- apache-spark - Spark Cassandra 写入行未在 cqlsh 中显示