我想根据最大时间戳获得最低价格。我只是为了把日期从桌子上拿下来。
我的查询应该是:
SELECT min(price) as price, timestamp, resellerid, tableid
FROM `sql_date_table`
WHERE tableid=%d
AND max(timestamp)
任何时候都将不胜感激。谢谢。
编辑:示例数据
tableid | resellerid | price | timestamp
-----------------------------------------
4 1 1549900 1516405599
4 1 2097042 1518618827
4 1 2107168 1519739181
4 2 1649900 1515352455
4 2 1649900 1518618508
4 2 1649900 1519739180
4 3 1700000 1520962427
4 3 1649900 1519828070
6 2 299400 1519738727
6 3 188800 1520962413
8 1 249900 1518618488
8 2 249500 1518618509
我们的想法是获得“最新爬行”的最低价格
答案 0 :(得分:1)
我怀疑你可能想要:
select t.*
from t
where tableid = ?
order by timestamp desc, price asc
limit 1;
答案 1 :(得分:1)
试试这个
SELECT * FROM `sql_date_table` WHERE `timestamp`
IN (SELECT min(`timestamp`) FROM `sql_date_table`
WHERE `tableid` = ? ORDER BY `price` DESC) LIMIT 1;
答案 2 :(得分:1)
这将从每个转销商的最新报价中返回最低价格:
SELECT Min(price)
FROM tab AS t1
JOIN
(
SELECT resellerid, Max(timestamp) AS max_ts
FROM tab
GROUP BY resellerid
) AS t2
ON t1.timestamp = t2.max_ts
由于您的问题不明确,您可能需要/需要将tableid
添加到GROUP BY。
如果您想要额外的列:
SELECT *
FROM tab AS t1
JOIN
(
SELECT resellerid, Max(timestamp) AS max_ts
FROM tab
GROUP BY resellerid
) AS t2
ON t1.timestamp_ = t2.max_ts
ORDER BY price
LIMIT 1
如果有多个经销商具有相同的价格,则会返回其中一个。
答案 3 :(得分:0)
答案 4 :(得分:0)
根据最新价格查找经销商中的最低价格:
select resellerid, price, timestamp
from sql_date_table
where
tableid = ?
and
timestamp = (select max(timestamp)
from sql_date_table t2
where t2.tableid = sql_date_table.tableid
and t2.resellerid = sql_date_table.resellerid)
order by price limit 1;
OR
select resellerid, price, timestamp
from sql_date_table
where
tableid = 4
and
timestamp = (select timestamp
from sql_date_table t2
where t2.tableid = sql_date_table.tableid
and t2.resellerid = sql_date_table.resellerid
order by timestamp desc limit 1)
order by price limit 1