【发布时间】:2020-09-08 11:49:12
【问题描述】:
有两个表:
create table author (id int primary key auto_increment, name varchar(255));
insert into author (name) values ('tingwei');
insert into author (name) values ('jiahui');
insert into author (name) values ('naidan');
insert into author (name) values ('weizhi');
insert into author (name) values ('siyao');
create table book (author_id int primary key auto_increment, book varchar(255));
insert into book (book) values ('I love you');
insert into book (book) values ('I hate you');
insert into book (book) values ('I miss you');
但是我不知道这个语句在“select”后面使用数字1是什么意思:
select *
from author
where exists (select 1 from book where book.author_id = author.id)
我已经在网上搜索了一些信息,但我一无所获。
【问题讨论】:
-
1 是一个虚拟列 .. 仅用于 sintax 并让查询工作 .. 如果满足内部子查询中的 where 子句,则该行存在并且解决了外部 where 条件
-
任何值都可以,不仅仅是
1。您可以使用0、-1、'a'或'My Little Pony'。没关系。即使是像null这样的非值也可以工作。 -
EXISTS 检查记录存在的事实。因此可以选择任何语法正确的列集。