mysql - 我无法对这部分 MySql 查询进行排序(升序/降序)?

标签 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

任何人请给我解决方案..

最佳答案

将其包装在另一个查询中,然后进行排序:

  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;

关于mysql - 我无法对这部分 MySql 查询进行排序(升序/降序)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13619452/

相关文章:

sql - MySQL 中的 UDF 性能

mysql - 更新一个表中某个字段的所有记录,该字段的值只存在于另一个表中

mysql - 使用jsf和mysql删除特定行TO DATATABLE

mysql - 在 MySQL 中获取表元数据

php - Laravel 没有将 Auth::id 插入表中

mysql - 当某些状态发生变化时,如何从日志表中获取时间戳?

python mysql连接

php - 关于 phpMyAdmin config.inc.php 文件的问题

mysql - Moodle 数据库无法连接

php - 将最新消息标记为... "new"