gpt4 book ai didi

sql - 列不存在于子查询表中,查询仍然运行良好,如何?

转载 作者:行者123 更新时间:2023-12-02 05:47:40 26 4
gpt4 key购买 nike

&它们的结构如下:

  1. 员工表的结构

    EmployeeID  LastName    FirstName   BirthDate   Photo   Notes
  2. 客户表的结构

    CustomerID  CustomerName    ContactName Address City    PostalCode  Country

问题:customers 表中不存在 lastname 列,这也是下面查询给出输出的原因。

select *
from employees
where lastname in (select lastname from customers)

有人能解释一下吗?

最佳答案

当查询中有多个表时,您应该始终限定列名。你打算:

select e.*
from employees e
where e.lastname in (select c.lastname from customers c);

这个查询会——实际上——产生一个无法识别的列错误。

但是,SQL 通常会帮助您在外部查询中“查找”值。这称为范围界定

编译器在customers 中查找lastname。它没有找到它,所以它决定你打算:

select e.*
from employees e
where e.lastname in (select e.lastname from customers c);

关于sql - 列不存在于子查询表中,查询仍然运行良好,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com