【问题标题】:What does it mean using number behind select?在选择后面使用数字是什么意思?
【发布时间】: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 检查记录存在的事实。因此可以选择任何语法正确的列集。

标签: mysql select exists


【解决方案1】:

SELECT 1 就是这样做的:它选择值1。当与WHERE EXISTS 一起使用时,子查询返回什么数据并不重要,只要它返回任何数据(换句话说:返回至少一行)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2023-01-19
    • 2013-08-07
    • 2013-07-08
    • 1970-01-01
    • 2010-10-03
    相关资源
    最近更新 更多