MySQL MySQL查询以订购两列,一列ASC另一列DESC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15313043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:49:54  来源:igfitidea点击:

MySQL query to order two column, one ASC another DESC

mysqlsqlphpmyadmin

提问by Cool Brain

I am trying to prepare a subject wise merit list. I am using this mysql query:

我正在尝试准备一个主题明智的优点列表。我正在使用这个mysql query

SELECT * 
FROM results 
ORDER BY qid ASC,
    marks DESC

Result is:

结果是:

enter image description here

在此处输入图片说明

But what I need is like this (look at marks column, I need to get same qidrows, ordered by marks):

但我需要的是这样的(看看标记列,我需要得到相同的qid行,按顺序排序marks):

enter image description here

在此处输入图片说明

Please anyone help me.

请任何人帮助我。

Update:And this is result.sqlfile to create the table in your pc.

更新:这是result.sql在您的电脑中创建表格的文件。

-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 09, 2013 at 05:40 PM
-- Server version: 5.5.27
-- PHP Version: 5.4.7

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `ges_omeca`
--

-- --------------------------------------------------------

--
-- Table structure for table `results`
--

CREATE TABLE IF NOT EXISTS `results` (
  `exam_id` int(11) NOT NULL AUTO_INCREMENT,
  `sid` varchar(50) NOT NULL,
  `qid` varchar(100) NOT NULL,
  `corrects` int(3) NOT NULL,
  `total_qs` int(3) NOT NULL,
  `marks` float NOT NULL,
  `date_time` datetime NOT NULL COMMENT 'DateTime when user submits the answer script.',
  PRIMARY KEY (`exam_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

--
-- Dumping data for table `results`
--

INSERT INTO `results` (`exam_id`, `sid`, `qid`, `corrects`, `total_qs`, `marks`, `date_time`) VALUES
(1, 'guest', 'EN_(Set-B)_(07.02.13)', 37, 40, 36.25, '2023-02-13 01:10:00'),
(2, 'guest', 'EN_(Set-B)_(07.02.13)', 11, 40, 10.25, '2013-02-23 01:56:58'),
(3, 'guest', 'P1_(Set-D)_(10.02.13)', 2, 100, 36.25, '2013-02-23 03:42:57'),
(4, 'guest', 'P1_(Set-B)_(09.02.13)', 5, 40, 5, '2013-02-23 03:46:59'),
(5, 'guest', 'EN_(Set-A)_(07.02.13)', 1, 40, 0.25, '2013-02-23 04:46:59'),
(6, 'guest', 'EN_(Set-A)_(07.02.13)', 6, 40, 5.5, '2013-02-23 04:59:59'),
(7, 'guest', 'P1_(Set-D)_(10.02.13)', 10, 100, 9.25, '2013-02-24 08:57:17'),
(8, 'guest', 'P1_(Set-B)_(09.02.13)', 5, 40, 5, '2013-02-24 01:23:50'),
(9, 'guest', 'EN_(Set-D)_(07.02.13)', 0, 40, -0.5, '2013-02-25 12:45:33'),
(10, 'guest', 'EN_(Set-D)_(07.02.13)', 2, 40, 1.5, '2013-02-25 01:45:38'),
(11, 'guest', 'P1_(Set-B)_(09.02.13)', 2, 40, 2, '2013-02-25 04:06:28'),
(12, 'guest', 'EN_(Set-C)_(07.02.13)', 5, 40, 4.5, '2013-02-25 04:42:27'),
(13, 'guest', 'P1_(Set-C)_(10.02.13)', 6, 40, 6, '2013-02-25 05:00:57');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

回答by whoone

This is exactly what you need:

这正是您所需要的:

SELECT * 
FROM results
ORDER BY SUBSTRING( qid
FROM 1 
FOR 1 ) ASC , marks DESC

回答by John Woo

I didn't really understand your question but looking on the result, I've noticed that records are sorted by qidwhich contains ENto be the first on the result list.

我并没有真正理解您的问题,但是查看结果时,我注意到记录按qid包含EN在结果列表中的第一个进行排序。

give this a try,

试试这个,

SELECT  *
FROM    tableName
ORDER   BY  CASE WHEN qid LIKE 'EN%' THEN 0 
                WHEN qid LIKE 'P1%' THEN 1
                ELSE 2
            END ASC,
            marks DESC

回答by uday

This might help you:

这可能会帮助您:

  SELECT *
    FROM results
ORDER BY SUBSTRING(qid FROM 1 FOR 9) ASC,
         marks DESC