[英]How to Select last Column in a row on mysql?
如何在mysql上选择一行的最后一列?
对于这样的表:
ID qty1 qty2 qty3 qty4 qty5 qty6 qty7
1 20 10 90 40 0 0 0
2 90 80 0 0 0 0 0
结果应为:
ID last_column_value
1 40
要么
ID last_column_value
2 80
如果需要1列的最后一个值,通常我会从表中使用Select Max(ID)。
我能看到解决的唯一方法是做一个讨厌的嵌套IF,如下所示。
SELECT ID, IF(qty7 <> 0,qty7, IF(qty6 <> 0,qty6, IF(qty5 <> 0,qty5, IF(qty4 <> 0,qty4, IF(qty3 <> 0,qty3, IF(qty2 <> 0,qty2, qty1)))))) last_column_value
FROM qtytable
这导致
ID LAST_COLUMN_VALUE
1 40
2 80
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.