我有一张这样的表
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
但是当我选择全部时它不是正确的值,它假设是1050,如下图所示
我的查询发生了什么?
答案 0 :(得分:1)
您正在将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
答案 1 :(得分:0)
请试试这个:
select * from Bids where bid_amount = ( select MAX(bid_amount) from Bids)
and i_id=1