请参阅此sqlfiddle.com/#!2/04c61/4(包含多列(十进制)的表格)。
我希望从该表中的所有单元格中获取最小值0
零值。
我试过这个sql
SELECT
LEAST(
MIN(IF(sgl_ro=0,'NULL',sgl_ro))
,MIN(IF(sgl_bb=0,'NULL',sgl_bb))
,MIN(IF(sgl_hb=0,'NULL',sgl_hb))
,MIN(IF(sgl_fb=0,'NULL',sgl_fb))
) AS MinRate
FROM room_rates
WHERE hotel_id='1'
GROUP BY hotel_id;
但是它给了我错误的值(我想因为有多行。如果有一行,它会返回正确的值)。
感谢。
答案 0 :(得分:2)
这是一个解决方案:
SELECT least(MIN(nullif(sgl_ro,0))
,MIN(nullif(sgl_bb,0))
,MIN(nullif(sgl_hb,0))
,MIN(nullif(sgl_fb,0)) ) as min_rate
FROM room_rates
WHERE hotel_id='1'
;
编辑:使用NULL而不是' NULL'
' NULL'是一个字符串,MySQL有关于如何在类型之间进行转换的非常奇怪的想法:
select case when 0 = 'NULL'
then 'ohoy'
else 'sailor'
end
from room_rates;
ohoy
ohoy
ohoy
即。你解决方案将删除'来自NULL:
SELECT
LEAST(
MIN(IF(sgl_ro=0,NULL,sgl_ro))
,MIN(IF(sgl_bb=0,NULL,sgl_bb))
,MIN(IF(sgl_hb=0,NULL,sgl_hb))
,MIN(IF(sgl_fb=0,NULL,sgl_fb))
) AS MinRate
FROM room_rates
WHERE hotel_id='1'
;
MINRATE
9
编辑:DBMS之间的比较:
我在sqlfiddle + DB2 10.5中测试了所有可用DBMS的以下场景:
create table t(x int);
insert into t(x) values (1);
select case when 0 = 'NULL'
then 'ohoy'
else 'sailor'
end
from t;
所有mysql版本都返回了&oh;'
sql.js返回' sailor'
所有其他人(包括DB2 10.5)都认为该查询是非法的。
编辑:处理行中所有列(或列的所有行)= 0
的情况select min(least(coalesce(nullif(sgl_ro,0), 2147483647)
,coalesce(nullif(sgl_bb,0), 2147483647)
,coalesce(nullif(sgl_hb,0), 2147483647)
,coalesce(nullif(sgl_fb,0), 2147483647) ) )
FROM room_rates
WHERE hotel_id='1'
AND coalesce(nullif(sgl_ro,0), nullif(sgl_bb,0)
,nullif(sgl_hb,0), nullif(sgl_fb,0)) IS NOT NULL;
答案 1 :(得分:1)
一步一步解决另一个问题:
从一列获取最小值:
SELECT MIN(sgl_ro) AS MinRate, hotel_id
FROM room_rates
WHERE sgl_ro != 0 AND hotel_id='1'
GROUP BY hotel_id
从所有列中获取最小值并聚合到最小值(最终查询):
SELECT MIN(MinRate) AS MinRate
FROM (
SELECT MIN(sgl_ro) AS MinRate, hotel_id
FROM room_rates
WHERE sgl_ro != 0 AND hotel_id='1'
GROUP BY hotel_id
UNION
SELECT MIN(sgl_bb) AS MinRate, hotel_id
FROM room_rates
WHERE sgl_bb != 0 AND hotel_id='1'
GROUP BY hotel_id
UNION
SELECT MIN(sgl_hb) AS MinRate, hotel_id
FROM room_rates
WHERE sgl_hb != 0 AND hotel_id='1'
GROUP BY hotel_id
UNION
SELECT MIN(sgl_fb) AS MinRate, hotel_id
FROM room_rates
WHERE sgl_fb != 0 AND hotel_id='1'
GROUP BY hotel_id
) res
GROUP BY hotel_id