为什么我在子查询和查询上得到不同的结果?
Why am I getting different results on a subquery and query?
我有以下 table 'client_ticket_thread
':
create table client_ticket_thread
(
id bigint unsigned auto_increment
primary key,
client bigint unsigned not null,
category varchar(255) collate utf8mb4_bin not null,
subject varchar(512) collate utf8mb4_bin not null,
message_count int unsigned default 0 not null,
client_read bit default b'1' not null,
closed bit default b'0' not null,
time_created int unsigned default unix_timestamp() not null,
time_last_message int unsigned default 0 not null,
time_closed int unsigned default 0 not null,
constraint client_ticket_thread_client_credential_id_fk
foreign key (client) references client_credential (id)
);
本table中的一个条目如下:
7,1,General Inquiry,Thread X,1,true,false,1641790706,1641790707,0
我正在尝试为特定目的编写查询,因此我需要来自其他 table 的外部数据,因此在子查询中,我正在尝试检索 closed
带有以下语句的字段:
select (select ctt.closed from client_ticket_thread ctt where ctt.id=7 and ctt.client=1 limit 1) as thread_active;
但是结果returnstrue
.
然而,下面的语句returnsfalse
,符合预期。
select closed as thread_active from client_ticket_thread where id=7 and client=1 limit 1;
当我尝试检索不同的字段时,例如subject
,我得到了正确的结果。可能导致此错误的原因是什么?
注意:我正在使用 MariaDB 10.7.1
。
问题是您使用的是位域。由于这似乎是一个标志,我建议您将其更改为布尔值。当使用布尔值时,它 returns 0/false 在每种情况下都符合预期。
这里 dbfiddle.uk 说明了我的意思。
create table client_ticket_thread
(
id bigint unsigned auto_increment
primary key,
client bigint unsigned not null,
category varchar(255) collate utf8mb4_bin not null,
subject varchar(512) collate utf8mb4_bin not null,
message_count int unsigned default 0 not null,
client_read bit default b'1' not null,
closed bit default b'0' not null,
client_read_bool boolean default true not null,
closed_bool boolean default false not null,
time_created int unsigned default unix_timestamp() not null,
time_last_message int unsigned default 0 not null,
time_closed int unsigned default 0 not null
);
insert into client_ticket_thread
values
(7,1,'General Inquiry','Thread X',1,1,0,true,false,1641790706,1641790707,0)
select (
select closed_bool
from client_ticket_thread ctt
where ctt.id=7
and ctt.client=1
) as thread_active;
我发现位字段实际上从子查询返回值 48,我想在你的情况下它被转换为 'true',而没有子查询则为 0。我不知道它为什么返回 48。
我通读了位数据类型 here 的文档。
Bits are returned as binary, so to display them, either add 0, or use
a function such as HEX, OCT or BIN to convert them.
所以你可以首先使用布尔值,或者 SELECT (SELECT closed+0 FROM ...
我有以下 table 'client_ticket_thread
':
create table client_ticket_thread
(
id bigint unsigned auto_increment
primary key,
client bigint unsigned not null,
category varchar(255) collate utf8mb4_bin not null,
subject varchar(512) collate utf8mb4_bin not null,
message_count int unsigned default 0 not null,
client_read bit default b'1' not null,
closed bit default b'0' not null,
time_created int unsigned default unix_timestamp() not null,
time_last_message int unsigned default 0 not null,
time_closed int unsigned default 0 not null,
constraint client_ticket_thread_client_credential_id_fk
foreign key (client) references client_credential (id)
);
本table中的一个条目如下:
7,1,General Inquiry,Thread X,1,true,false,1641790706,1641790707,0
我正在尝试为特定目的编写查询,因此我需要来自其他 table 的外部数据,因此在子查询中,我正在尝试检索 closed
带有以下语句的字段:
select (select ctt.closed from client_ticket_thread ctt where ctt.id=7 and ctt.client=1 limit 1) as thread_active;
但是结果returnstrue
.
然而,下面的语句returnsfalse
,符合预期。
select closed as thread_active from client_ticket_thread where id=7 and client=1 limit 1;
当我尝试检索不同的字段时,例如subject
,我得到了正确的结果。可能导致此错误的原因是什么?
注意:我正在使用 MariaDB 10.7.1
。
问题是您使用的是位域。由于这似乎是一个标志,我建议您将其更改为布尔值。当使用布尔值时,它 returns 0/false 在每种情况下都符合预期。
这里 dbfiddle.uk 说明了我的意思。
create table client_ticket_thread
(
id bigint unsigned auto_increment
primary key,
client bigint unsigned not null,
category varchar(255) collate utf8mb4_bin not null,
subject varchar(512) collate utf8mb4_bin not null,
message_count int unsigned default 0 not null,
client_read bit default b'1' not null,
closed bit default b'0' not null,
client_read_bool boolean default true not null,
closed_bool boolean default false not null,
time_created int unsigned default unix_timestamp() not null,
time_last_message int unsigned default 0 not null,
time_closed int unsigned default 0 not null
);
insert into client_ticket_thread
values
(7,1,'General Inquiry','Thread X',1,1,0,true,false,1641790706,1641790707,0)
select (
select closed_bool
from client_ticket_thread ctt
where ctt.id=7
and ctt.client=1
) as thread_active;
我发现位字段实际上从子查询返回值 48,我想在你的情况下它被转换为 'true',而没有子查询则为 0。我不知道它为什么返回 48。
我通读了位数据类型 here 的文档。
Bits are returned as binary, so to display them, either add 0, or use a function such as HEX, OCT or BIN to convert them.
所以你可以首先使用布尔值,或者 SELECT (SELECT closed+0 FROM ...