2015-09-04 27 views
-1

我有一个简单的数据是这样的:选择与条件SQL表中最后一个值

enter image description here

现在我想曾经有USER_ID = 41和最高价最新的数据,输出是这样的:

enter image description here

我怎么用SQL命令行来做到这一点?

感谢阅读

+0

是你的输出是正确的,因为你是在说USER_ID = 41,但输出显示42甚至2倍。 –

+0

您的条件和您的示例输出不匹配 –

+0

如果我能够理解您的要求,可能是我的第二个查询满足您的输出要求。 –

回答

2

尝试此查询

select User_id,auction_id,price from tablename where price in(select price from tablename where id in(select max(id) from tablename group by user_id)) 
+0

哦!非常感谢你 !工作! – Thanhtu150

0

如果已经更新输出错误,然后尝试按你提供的条件 -

select user_id,auction_id,price 
from mytable 
where user_id=41 
order by auction_id; 

但如果按你的输出你想要的user_id 1个最新行= 41,然后最高价的所有行,那么你可以尝试一下它 -

SELECT user_id,auction_id,price 
FROM mytable 
WHERE user_id=41 
ORDER BY id DESC LIMIT 1 
UNION 
SELECT b.user_id,b.auction_id,price 
FROM mytable b 
JOIN (
SELECT MAX(price) AS price 
FROM mytable 
) a ON a.price=b.price; 
0

根据你的输出,你想试试这个:

select user_id,auction_id,price 
from table_name 
where user_id=41 
order by id desc; 
相关问题