PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Return specific row and desc order in sql in php

 February 22, 2022     mysql, php, phpmyadmin, sql     No comments   

Issue

i want my query to return a specific row of the table where a column contains specific value first and with year desc.

if i have table something like this

Table employee

 id  - Name    -    Year - Status 
  1  - Ashish  -    2016 - Old
  2  - Srisan  -    2017 - New
  3  - Mohit   -    2018 - New
  4  - Ram     -    2015 - Old
  5  - Boby    -    2016 - New

then result should be

 id -  Name   -  Year - Status 
  3 -  Mohit  -  2018 - New
  2 -  Srisan -  2017 - New
  5 -  Boby   -  2016 - New
  1 -  Ashish -  2016 - Old
  4 -  Ram    -  2015 - Old

Where Status is new and sort via desc of Year


Solution

You can try below -

select * from tablename
order by case when 'New' then 0 else 1 end, year desc


Answered By - Fahmi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,209,932

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing