1

我在 mysql 中使用 MIN 函数来返回列的最小值 - 让我们称之为价格,但是我也希望为该特定行显示价格最小值的各种其他列。到目前为止,我有以下内容:

SELECT underlying,volatility FROM shares
where (id,price) IN (
    SELECT id,MIN(price) FROM shares
    where DATE(issueDate) >= DATE(NOW())
    group by id
 )

这几乎可以工作,但是它返回不止一行,我只想显示最后一行,有人可以帮我吗?

4

3 回答 3

1

我想我已经解决了这个问题,执行需要一段时间,所以我将从添加一个新索引开始:

SELECT underlying,volatility,price FROM shares
where (id,price) IN (
    SELECT id,MIN(price) FROM shares
    where DATE(issueDate) >= DATE(NOW())
    group by id
) order by price asc limit 1;
于 2013-11-07T01:43:30.260 回答
0
SELECT underlying,volatility FROM shares
where (id,price) IN (
    SELECT 
        SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY price ASC), ',', 1) AS id,
        MIN(price) FROM shares
    where DATE(issueDate) >= DATE(NOW())
    group by id
 )
于 2013-11-07T08:50:59.527 回答
0

有子句可以解决问题,请检查我的解决方案

select volatility,underlying, min(price) as min_price  from shares
where DATE(issueDate) >= DATE(NOW())
group by issueDate
having min_price = min(price);

小提琴示例

于 2013-11-07T09:35:21.567 回答