我想返回SELECT
中使用max()
的每個行的日期列。或者也許有更好的方法來做到這一點?SQL:返回SELECT()中指定的max()行的列
這是我的想象:
SELECT
MAX(time) as time, [date column from max(time) row] as timedate,
MAX(distance) as distance, [date column from max(distance) row] as distancedate,
MAX(weight) as weight, [date column from max(weight) row] as weightdate
這裏是我當前的SQL,這並不返回日期爲每個MAX()的行。
$db->query("SELECT e.id as id, e.name, MAX(ue.time) as time, MAX(ue.weight) as weight, MAX(ue.distance) as distance
FROM `users exercises` as ue
LEFT JOIN `exercises` as e ON exerciseid = e.id
GROUP BY e.id
LIMIT 30");
id | exerciseid | date | weight | distance | time
----------------------------------------------------------
1 | 1 | 2014-06-14 | 100 | 33 | null
2 | 1 | 2013-03-03 | 500 | 11 | null
3 | 1 | 2014-11-11 | null | null | 41
電流輸出:
Array
(
[id] => 1
[name] => run
[time] => 41
[weight] => 500
[distance] => 33
)
預期輸出:
Array
(
[id] => 1
[name] => run
[time] => 41
[time_date] => 2014-11-11
[weight] => 500
[weight_date] => 2013-03-03
[distance] => 33
[distance_date] => 2014-06-14
)
SQL小提琴:http://sqlfiddle.com/#!2/75e53/1
我不知道我明白你想達到什麼。你能包括一些樣本數據和你的預期結果嗎? – Mureinik
我已經更新了這個問題。柏油。 – ditto