[英]Selecting a record where data in anouther column is at min
我需要在兩列中獲得兩個數字的記錄。 但是我需要找到其中一列的最小值。 並找到與該最小數字對齊的數字。
現在,我有以下內容:
$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.
如果您要查找一行,則最簡單的方法是order by
and limit
:
select t.*
from t
order by t.col2 asc
limit 1;
您需要使用子查詢。
Select ID from my_table t, (select min(price) as minPrice from my_table) where t.price =minPrice;
沒有測試過,但應該可以。
它以最低價格返回所有ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.