2014-07-12 34 views
0

我想返回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

+0

我不知道我明白你想達到什麼。你能包括一些樣本數據和你的預期結果嗎? – Mureinik

+0

我已經更新了這個問題。柏油。 – ditto

回答

1
SELECT e.id as id, e.name, 

    MAX(ue.time) as time, 
    (
     select date 
     from `users exercises` 
     WHERE time = MAX(ue.time) AND ue.`userid` = $userid 
     LIMIT 1 
    ) as time_date, 

    MAX(ue.weight) as weight, 
    (
     select date 
     from `users exercises` 
     WHERE weight = MAX(ue.weight) AND ue.`userid` = $userid 
     LIMIT 1 
    ) as weight_date, 

    MAX(ue.distance) as distance, 
    (
     select date 
     from `users exercises` 
     WHERE distance = MAX(ue.distance) AND ue.`userid` = $userid 
     LIMIT 1 
    ) as distance_date 

FROM `users exercises` as ue 
LEFT JOIN `exercises` as e ON exerciseid = e.id 
WHERE ue.`userid` = $userid 
GROUP BY e.id 
LIMIT 30 
0

有可能是一個更有效的方式來做到這一點,但遺憾的是米y MySQL技能不太好;然而,下面的代碼你想要做什麼:使用支持partition by沒有實現一個更好的方式分貝

解決方案1 ​​

select 
    mx.time 
, t.date as timedate 
, mx.distance 
, d.date as distancedate 
, mx.weight 
, w.date as weightdate 
from 
(
    SELECT 
     MAX(`time`) as `time` 
    , MAX(`distance`) as `distance` 
    , MAX(`weight`) as `weight` 
    from `users exercises` 
) as mx 
inner join `users exercises` as t on t.time = mx.time 
inner join `users exercises` as d on d.distance = mx.distance 
inner join `users exercises` as w on w.weight = mx.weight; 

解決方案2

select 
    mx.time 
, (select date from `users exercises` as x where x.time = mx.time limit 1) as timedate 
, mx.distance 
, (select date from `users exercises` as y where y.distance = mx.distance limit 1) as distancedate 
, mx.weight 
, (select date from `users exercises` as z where z.weight = mx.weight limit 1) as weightdate 
from 
(
    SELECT 
     MAX(`time`) as `time` 
    , MAX(`distance`) as `distance` 
    , MAX(`weight`) as `weight` 
    from `users exercises` 
) as mx; 

對於任何人這個;可悲的是,MySQL目前不支持該功能。

SQL小提琴:http://sqlfiddle.com/#!2/75e53/13