[英]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.