gpt4 book ai didi

mysql子查询字段不存在但查询全部

转载 作者:行者123 更新时间:2023-11-29 17:13:35 32 4
gpt4 key购买 nike

mysql> select id,mr_id,type from oh_mr_ocr_sub_info where mr_id in

(select mr_id from oh_mr_base_info) limit 2;

+----+-------+--------------+
| id | mr_id | type |
+----+-------+--------------+
| 2 | 7 | inhospital |
| 3 | 7 | chemotherapy |
+----+-------+--------------+

2 rows in set (0.01 sec)

mysql> select mr_id from oh_mr_base_info;

ERROR 1054 (42S22): Unknown column 'mr_id' in 'field list'

oh_mr_base_info 不包含字段mr_id;但返回所有 oh_mr_ocr_sub_info 数据?为什么?

最佳答案

这就是 SQL 的工作方式:)

如果为表指定别名,您将看到查询将不再起作用

select a1.id,
a1.mr_id,
a1.type
from oh_mr_ocr_sub_info a1
where a1.mr_id in (select b1.mr_id
from oh_mr_base_info b1)
limit 2;

上面会给你一个错误,说b1.mr_id是一个无效的列。但是,如果您编写 a1.mr_id ,它将毫无问题地工作。

这并不完全与表别名有关,而是与表列有关。可以看出mr_id不需要存在于B1中,它也可以存在于a1中。与选择常量相同

select 111 from oh_mr_base_info;

只要 oh_mr_base_info 中有任意数量的可用记录,查询就会返回 111。始终建议对此类问题使用表别名

关于mysql子查询字段不存在但查询全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51760667/

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