SQL:返回SELECT中max()行的指定列

时间:2014-07-12 08:54:28

标签: mysql sql max

我想为max()中使用SELECT的每一行返回日期列。或者也许有更好的方法来做到这一点?

这就是我的想象:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这可能是一种更有效的方法,但遗憾的是我的MySQL技能并不那么好;但是下面的代码可以满足您的需求:

解决方案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;

对于任何使用支持partition by的数据库的人来说,有更好的方法来实现它;遗憾的是MySQL目前不支持该功能。

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