选择查询没有返回正确的值

时间:2018-05-10 09:42:25

标签: mysql database

我有一张这样的表

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

返回值为950,如图中所示 slect max bidamout

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

enter image description here

我的查询发生了什么?

2 个答案:

答案 0 :(得分:1)

您正在将bid_amount存储为文本,这不是一个好习惯,我建议您更改结构并指定正确的整数/双精度类型。现在,您需要将列转换为数字,然后才能获得正确的结果。

快速破解不是解决方案

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

Demo

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

Demo

答案 1 :(得分:0)

请试试这个:

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