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。始终建议对此类问题使用表别名