Skip to content

Instantly share code, notes, and snippets.

@mrabbani
Last active November 5, 2019 05:39
Show Gist options
  • Save mrabbani/2092fae872a5c6fd915cf02541d0d01f to your computer and use it in GitHub Desktop.
Save mrabbani/2092fae872a5c6fd915cf02541d0d01f to your computer and use it in GitHub Desktop.
Mysql tricks

MySql fetch data by conditional order

Suppose, You want to search product with specified key and the products with stock should appear first in the search result.

SELECT * FROM `products` WHERE name LIKE 'na%'
ORDER BY CASE
    WHEN id IN (602, 603, 18125) THEN 1
    ELSE 2
END, name ASC

Add Virtual field

//add p_type as virtual field
SELECT name, 'mahbub' as p_type from products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment