选择具有最小值的多个列

时间:2016-09-30 14:26:33

标签: mysql sql

示例模式:

id val1 val2 val3 val4 val5 mid
1   5     6    7    1    4   1
4   7     8    5    9    5   1
5   4     1    4    2    7   2
7   3     2    8    7    2   1

以下是我正在尝试的内容,但此查询仅返回包含所有值的一行:

SELECT id, MIN(val1), MIN(val2), MIN(val3), MIN(val4), MIN(val5) FROM table WHERE mid=1 

我想要结果(对于每个val列选择id,min为min(valx),其中mid = 1):

id min
7   3
7   2
4   5
1   1
7   2

一个注意事项:列数最多可达50(valx,其中x是1到50之间的数字)。

6 个答案:

答案 0 :(得分:1)

SELECT id, minimum
FROM
    (SELECT MIN(Val1) as minimum FROM TableName WHERE Mid = 1) v1
    INNER JOIN TableName t1
    ON v1.minimum = t1.Val1
    AND t1.Mid = 1

UNION ALL

SELECT id, minimum
FROM
    (SELECT MIN(Val2) as minimum FROM TableName WHERE Mid = 1) v2
    INNER JOIN TableName t2
    ON v2.minimum = t2.Val2
    AND t2.Mid = 1

UNION ALL

SELECT id, minimum
FROM
    (SELECT MIN(Val3) as minimum FROM TableName WHERE Mid = 1) v3
    INNER JOIN TableName t3
    ON v3.minimum = t3.Val3
    AND t3.Mid = 1

UNION ALL

SELECT id, minimum
FROM
    (SELECT MIN(Val4) as minimum FROM TableName WHERE Mid = 1) v4
    INNER JOIN TableName t4
    ON v4.minimum = t4.Val4
    AND t4.Mid = 1

UNION ALL

SELECT id, minimum
FROM
    (SELECT MIN(Val5) as minimum FROM TableName WHERE Mid = 1) v5
    INNER JOIN TableName t5
    ON v5.minimum = t5.Val5
    AND t5.Mid = 1

这只是一个令人讨厌的令人讨厌的查询和情况,但我想出了你想要的东西。如果你改进你的叙述,你可能会得到比这个更好的答案。

这就是我想要的结果和叙述。

找到Mid = 1的每列的最小值,然后找到与该最小值相关的ID。以上结果是:

id  minimum
7   3
7   2
4   5
1   1
7   2

注意你实际拥有的5 5对5 4但是5将是Val3中的最小值,因为4表示的是mid = 2.所以值5的ID是4 ....

答案 1 :(得分:1)

select id, val1 from table where val1=(select min(val1) from table where mid=1) and mid=1
union all
select id, val2 from table where val2=(select min(val2) from table where mid=1) and mid=1
union all
select id, val3 from table where val3=(select min(val3) from table where mid=1) and mid=1
union all
select id, val4 from table where val4=(select min(val4) from table where mid=1) and mid=1
union all
select id, val5 from table where val5=(select min(val5) from table where mid=1) and mid=1

答案 2 :(得分:0)

使用LEAST()功能...

<?php
function feedburner_img_resize($the_content) {
// Create a new istance of DOMDocument
$post = new DOMDocument();
// Load $the_content as HTML
$post->loadHTML($the_content);
// Look up for all the <img> tags.
$imgs = $post->getElementsByTagName('img');

// Iteration time
foreach( $imgs as $img ) {    

    // if width is smaller than 600 - no need to continue
    $width = $img->getAttribute('width');
    if( $width < 600 ) continue;

    $img->removeAttribute('width');
    $img->setAttribute('width', 600);
    $img->removeAttribute('height'); // so the image is not distorted
};

 return $post->saveHTML();
}

add_filter('the_content_rss', 'feedburner_img_resize');
?>

答案 3 :(得分:0)

如果我理解你的问题,我希望它会对你有所帮助

 SELECT id, MIN(minimum) as minimum FROM (
 SELECT id, val1 As minimum FROM table where  mid=1
 UNION ALL
 SELECT id, val2 As minimum FROM table where  mid=1
 UNION ALL
 SELECT id, val3 As minimum FROM table where  mid=1
 union All
 SELECT id, val4 As minimum FROM table  where  mid=1
 union ALL
 SELECT id, val5 As minimum FROM table where  mid=1
 ) As minvalue
 GROUP BY id 

答案 4 :(得分:0)

对不起上一个回答,没有看到你的结果。 我希望这能给你带来结果。

;with cte as(
    Select a.ID
        , t.Val
        , ROW_NUMBER() over(partition by a.id order by t.ColNo) as ColNo
    From @TABLE as a
    Outer apply (Values (1,Val1), (2,Val2),(3,Val3),(4,Val4), (5,Val5) ) t(ColNo,Val)
    Where a.MID=1
)
, cte2 as( 
    Select ColNo, MIN(val) as MinVal 
    From cte
    Group by ColNo
) 
    Select a.ID, a.Val from cte as a
    Inner join cte2 as b on a.ColNo=b.ColNo and a.Val=b.MinVal
    Order by a.ColNo

答案 5 :(得分:-1)

SELECT id, MIN(val1), MIN(val2), MIN(val3), MIN(val4), MIN(val5) 
FROM table 
WHERE mid=1 
GROUP by id