R
R
Roman Kuzmenko2015-12-10 03:04:52
MySQL
Roman Kuzmenko, 2015-12-10 03:04:52

MySQL. How to make a selection / sorting?

Hello! Please tell me how to organize the sample.
There is a table (for example)
id name upc
1 a1 new
2 a2 new
3 a3 old
4 a4 action
5 a5 old
When selecting all records, how to do it correctly so that those records with upc = old are displayed first?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SagePtr, 2015-12-10
@SagePtr

SELECT * FROM table ORDER BY upc = 'old' DESC;

A
Alexander Zubarev, 2015-12-10
@zualex

You can even sort by multiple values:

SELECT * FROM table ORDER BY FIELD (upc, 'old', 'new', 'action') DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question