[英]How to Select last Column in a row on mysql?
How to Select last Column in a row on mysql? 如何在mysql上选择一行的最后一列?
For a table like this: 对于这样的表:
ID qty1 qty2 qty3 qty4 qty5 qty6 qty7
1 20 10 90 40 0 0 0
2 90 80 0 0 0 0 0
The result should be: 结果应为:
ID last_column_value
1 40
or 要么
ID last_column_value
2 80
Usually I use Select Max(ID) from table if I need last value on 1 column. 如果需要1列的最后一个值,通常我会从表中使用Select Max(ID)。
The only way I can see to solve it is to do a nasty nested IF as below. 我能看到解决的唯一方法是做一个讨厌的嵌套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
This results in 这导致
ID LAST_COLUMN_VALUE
1 40
2 80
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.