我想得到一组行,如果col_3在某些条件下,并且col_1的最大值大于5,则返回col_1,col_2,col_3的4个col_1,如果值小于5,则返回colrows按col_2排序
如果max(col_1)> 5
select col_1, col_2, col_3 from TABLE where col_3 = 'some condition' order by col_1 desc limit 4;
否则
select col_1, col_2, col_3 from TABLE where col_3 = 'some condition' order by col_2 desc limit 4;
它可以很好地分开,但是下一个查询只返回一行,按col_2排序。
select col_1, col_2, col_3 from TABLE where col_3 = 'some condition' order by if(max(col_1) > 5, col_1, col_2) desc limit 4;
对不起我的短英文... 再次感谢您的帮助 祝你有愉快的一天:)
答案 0 :(得分:0)
尝试(SELECT max(col_1) FROM TABLE) > 5
而不是max(col_1) > 5
select col_1, col_2, col_3 from TABLE where col_3 = 'some condition' order by if((SELECT max(col_1) FROM TABLE) > 5, col_1, col_2) desc limit 4;