在条件中选择SQL表中的最后一个值

时间:2015-09-04 05:48:06

标签: mysql

我有这样的示例数据:

enter image description here

现在我想得到最新的数据,用于拥有user_id = 41和最高价格,输出如下:

enter image description here

如何使用SQL命令行执行此操作?

感谢阅读

3 个答案:

答案 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))

答案 1 :(得分:0)

如果您错误地更新了输出,请按照提供的条件尝试 -

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

但是如果根据你的输出你需要1行最新的user_id = 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;

答案 2 :(得分:0)

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

select user_id,auction_id,price 
from table_name 
where user_id=41 
order by id desc;