Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query to order two column, one ASC another DESC

I am trying to prepare a subject wise merit list. I am using this 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 qid rows, ordered by marks):

enter image description here

Please anyone help me.

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

-- 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 */;
like image 271
Cool Brain Avatar asked Mar 09 '13 16:03

Cool Brain


People also ask

How do I sort by both ASC and DESC in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

Can we use ORDER BY for 2 columns?

Discussion: If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY . This clause comes at the end of your SQL query.

How do I order two columns in descending order in SQL?

To sort the records in descending order, use the DESC keyword. Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first.

How do I sort multiple columns in MySQL?

Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.


Video Answer


2 Answers

This is exactly what you need:

SELECT * 
FROM results
ORDER BY SUBSTRING( qid
FROM 1 
FOR 1 ) ASC , marks DESC
like image 67
whoone Avatar answered Oct 29 '22 02:10

whoone


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

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
like image 36
John Woo Avatar answered Oct 29 '22 01:10

John Woo