【问题标题】:Selecting a record where data in anouther column is at min选择另一列中的数据为 min 的记录
【发布时间】:2016-05-24 22:48:11
【问题描述】:

我需要在两列中获取两个数字的记录。但我需要找到其中一列的最小值。并找到与该最小数字对齐的数字。

现在我有以下内容:

$sql = "SELECT ID, MIN(price) AS minPrice FROM my_table";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

echo $row["minPrice"]; // This works
echo $row["ID"]; // This is not the number that is in the record where minPrice is.

【问题讨论】:

    标签: php mysql select mysqli min


    【解决方案1】:

    您需要使用子查询。

    Select ID from my_table t, (select min(price) as minPrice from my_table) where t.price =minPrice;
    

    没有测试过,但应该可以。

    它以最低价格返回所有 id。

    【讨论】:

      【解决方案2】:

      如果要找一行,最简单的方法是order bylimit

      select t.*
      from t
      order by t.col2 asc
      limit 1;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-31
        • 2012-03-18
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 2021-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多