簡體   English   中英

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

[英]SQL: Return specified column of max() row inside SELECT

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

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

可能有一種更有效的方法來執行此操作,但可悲的是,我的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的db的任何人partition by都有一種更好的方法來實現; 遺憾的是,MySQL目前不支持該功能。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM