select查询没有返回正确的值

xzabzqsa  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(321)

我有一张这样的table

create table Bids (
b_id INT not null auto_increment PRIMARY KEY,
u_id integer not null,
foreign key (u_id) references Users(u_id),
i_id integer not null,
foreign key (i_id) references Items(i_id),
bid_amount long,
}

然后我运行查询

Select * from Bids Where bid_amount in ( Select MAX(bid_amount) from Bids) 
and i_id=1

返回值如图所示

但当我选择all时,它不是正确的值,它假设为1050,如下图所示

我的问题怎么了?

31moq8wy

31moq8wy1#

请试试这个:

select * from Bids where bid_amount = ( select MAX(bid_amount) from Bids) 
and i_id=1
pdtvr36n

pdtvr36n2#

您正在存储 bid_amount 作为文本,这不是一个好的做法,我建议你改变你的结构,并指定适当的整数/双精度类型。现在,您需要将列转换为数字,然后才能得到正确的结果。
一个快速的黑客不是解决办法

Select * from Bids 
Where bid_amount in ( Select MAX(bid_amount + 0) from Bids) 
and i_id=1

演示

Select * from Bids 
Where bid_amount in ( Select MAX(CAST(bid_amount as DECIMAL(9,2))) from Bids) 
and i_id=1

或者

Select * from Bids 
Where bid_amount in ( Select MAX(CONVERT(bid_amount ,UNSIGNED INTEGER) ) from Bids) 
and i_id=1

演示

相关问题