MySQL IN 子句中的负值有什么问题?

发布于 2024-10-19 14:50:31 字数 1341 浏览 1 评论 0原文

我有这样的视图:

TABLE_TYPE
(code,name)
(1,'Computer')
(2,'Television')
(3,'Radio')

TABLE_THING
(code,name,type=TABLE_TYPE:code)
(-9999, 'ThingFirst',1)
(1,'Thing1',1)
(2,'Thing2',2)
(3,'Thing3',2)
(4,'Thing4',3)
(5,'Thing5',1)

VIEW_THINGS
(code,name,thingtype)
(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

查询:

select * from VIEW_THINGS where code in (-9999,1,2,3,4,5)

结果是:

(-9999, 'ThingFirst',NULL)

结果应该是:

(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

出了什么问题???

提示 1:当我从 IN 子句中退出负值(-9999)时,我得到:

(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

提示 2:如果我从总视图结果中进行选择,我会得到正确的数据:

select * from (select * from VIEW_THINGS as T) where code in (-9999,1,2,3,4,5)

结果:

(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

I have a view like this:

TABLE_TYPE
(code,name)
(1,'Computer')
(2,'Television')
(3,'Radio')

TABLE_THING
(code,name,type=TABLE_TYPE:code)
(-9999, 'ThingFirst',1)
(1,'Thing1',1)
(2,'Thing2',2)
(3,'Thing3',2)
(4,'Thing4',3)
(5,'Thing5',1)

VIEW_THINGS
(code,name,thingtype)
(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

Query:

select * from VIEW_THINGS where code in (-9999,1,2,3,4,5)

Result is:

(-9999, 'ThingFirst',NULL)

Result should be:

(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

WHAT'S WRONG????

Tip1: When I quit the negative value (-9999) from the IN clause I get this:

(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

Tip2: If I do the select from the total view results I get the correct data:

select * from (select * from VIEW_THINGS as T) where code in (-9999,1,2,3,4,5)

Results:

(-9999, 'ThingFirst','Computer')
(1,'Thing1','Computer')
(2,'Thing2','Television')
(3,'Thing3','Television')
(4,'Thing4','Radio')
(5,'Thing5','Computer')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶ ̄淡然 2024-10-26 14:50:31

完整的测试脚本:

create table TABLE_TYPE(code int,name varchar(20));
insert table_type values
(1,'Computer'),
(2,'Television'),
(3,'Radio');

create table TABLE_THING(code int,name varchar(20),type int);
insert table_thing values
(-9999, 'ThingFirst',1),
(1,'Thing1',1),
(2,'Thing2',2),
(3,'Thing3',2),
(4,'Thing4',3),
(5,'Thing5',1);

create table VIEW_THINGS(code int, name varchar(20),thingtype varchar(20));
insert view_things values
(-9999, 'ThingFirst','Computer'),
(1,'Thing1','Computer'),
(2,'Thing2','Television'),
(3,'Thing3','Television'),
(4,'Thing4','Radio'),
(5,'Thing5','Computer');

select * from VIEW_THINGS where code in (-9999,1,2,3,4,5);

输出

"code";"name";"thingtype"
"-9999";"ThingFirst";"Computer"
"1";"Thing1";"Computer"
"2";"Thing2";"Television"
"3";"Thing3";"Television"
"4";"Thing4";"Radio"
"5";"Thing5";"Computer"

既然这样有效,我就赌 VIEW_THINGS 是一个连接两个表的视图。让我们看看

create view view_things2
as
select t.code, t.name, y.name thingtype
from table_thing t
inner join table_type y on y.code = t.type
;
select * from VIEW_THINGS2 where code in (-9999,1,2,3,4,5);

输出:如上所述,

这是我们所能提供的信息。

Full test script:

create table TABLE_TYPE(code int,name varchar(20));
insert table_type values
(1,'Computer'),
(2,'Television'),
(3,'Radio');

create table TABLE_THING(code int,name varchar(20),type int);
insert table_thing values
(-9999, 'ThingFirst',1),
(1,'Thing1',1),
(2,'Thing2',2),
(3,'Thing3',2),
(4,'Thing4',3),
(5,'Thing5',1);

create table VIEW_THINGS(code int, name varchar(20),thingtype varchar(20));
insert view_things values
(-9999, 'ThingFirst','Computer'),
(1,'Thing1','Computer'),
(2,'Thing2','Television'),
(3,'Thing3','Television'),
(4,'Thing4','Radio'),
(5,'Thing5','Computer');

select * from VIEW_THINGS where code in (-9999,1,2,3,4,5);

Output

"code";"name";"thingtype"
"-9999";"ThingFirst";"Computer"
"1";"Thing1";"Computer"
"2";"Thing2";"Television"
"3";"Thing3";"Television"
"4";"Thing4";"Radio"
"5";"Thing5";"Computer"

Since that works, I'll take a punt that VIEW_THINGS is a view that joins the two tables. Let's see

create view view_things2
as
select t.code, t.name, y.name thingtype
from table_thing t
inner join table_type y on y.code = t.type
;
select * from VIEW_THINGS2 where code in (-9999,1,2,3,4,5);

Output: as above

This is as far as we can go with the info you have provided.

静若繁花 2024-10-26 14:50:31

你尝试过改变顺序吗

SELECT * FROM VIEW_THINGS WHERE code IN (1,2,3,4,5,-9999)

did you try with changing order like

SELECT * FROM VIEW_THINGS WHERE code IN (1,2,3,4,5,-9999)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文