Is it possible to sort the order in which columns are returned in a mysql SELECT statement?

前端 未结 2 1924
既然无缘
既然无缘 2021-01-27 16:21

Imagine two questions from an online survey:

  1. Do you like apples?

Result stored in mysql db column \"q1\" as

2条回答
  •  遇见更好的自我
    2021-01-27 16:55

    Assuming I'm understanding your question correctly, you could use a case statement:

    SELECT CASE WHEN SUM(q1) > SUM(q2) THEN SUM(q1) ELSE SUM(q2) END AS q1Sum,
       CASE WHEN SUM(q1) > SUM(q2) THEN SUM(q2) ELSE SUM(q1) END AS q2Sum 
    FROM myTable 
    

    Actually this would mislabel the columns -- you cannot alter the name of the column without using dynamic sql.

提交回复
热议问题