2012-11-29 91 views
1

我想排序這些元素升序/降序。查詢我不能排序(升序/降序)這部分的MySql查詢?

部分:

sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END) as optcount1, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END) as optcount2, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END) as optcount3, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END) as optcount4, 

全面查詢:

SELECT 39 AS ques_id,215 as optid1,216 as optid2,217 as optid3,218 as optid4, 
'Easy to start the business' as optans1, 
'Lower tax rate than a corporation' as optans2, 
'Liability is shared' as optans3, 
'Owner has total control and say over business' as optans4, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END) as optcount1, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END) as optcount2, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END) as optcount3, 
sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END) as optcount4, 
'217' as answer, 4 as count FROM `user_training_answers_statistics` as u, 
answers as a WHERE a.question_id='39' AND u.answer_id=a.answer_id 

請人給我這種情況的解決方案..

回答

0

敷在另一個查詢然後執行排序:

select * from 
    (SELECT 39 AS ques_id,215 as optid1,216 as optid2,217 as optid3, 
      218 as optid4, 'Easy to start the business' as optans1, 
      'Lower tax rate than a corporation' as optans2, 
      'Liability is shared' as optans3, 
      'Owner has total control and say over business' as optans4, 
      sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END) 
      as optcount1, 
      sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END) 
      as optcount2, 
      sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END) 
      as optcount3, 
      sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END) 
      as optcount4, '217' as answer, 4 as count 
    FROM user_training_answers_statistics as u, 
      answers as a 
    WHERE a.question_id='39' AND u.answer_id=a.answer_id) as tmpTable 
ORDER BY optcount1, optcount2, optcount3, optcount4 ASC; 
+0

嗨,感謝您的回覆,但這裏optcount是動態值,它可能是2或4或6類似的東西。在這種情況下,我如何分類這個值。 – RaJeSh

+0

$ sql =「SELECT $ qid AS ques_id,$ optid $ optname $ option'$ corrctans'as answer,$ j as'user_training_answers_statistics'作爲您的計數,作爲WHERE回答a.question_id ='$ qid'AND u。 answer_id = a.answer_id \t「;
這些是動態值:$ optid $ optname $選項。 – RaJeSh