L
L
Lev K2016-09-15 22:46:26
MySQL
Lev K, 2016-09-15 22:46:26

How can I set my own sorting rule for a query?

Good day!
There is a plate in which the history of clients (companies) contacting for the provision of services is stored. For definiteness, the table has fields: id, id_company, date. I set the usual query to this table and sort by the date of the last access (nothing special).
I asked myself this question, is it possible to set your own sorting parameters for the query result so that the same company does not stand in a row, if possible. Something like this:
id_company | date
1-------------1.09.2016
2-------------29.08.2016
3-------------26.08 .2016
1-------------30.08.2016
2-------------28.08.2016
3------------- 25.08.2016
1-------------29.08.2016
3-------------22.08.2016
3-------------08/20/2016
Ideally, this should be all in one request (by date and its own sorting)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-09-15
@Rsa97

ORDER BY `date`, `id_company`

A
Alexander, 2016-09-15
@NeiroNx

So that the same company does not stand in a row - the grouping is called,
you can try this:

SELECT id_company,MAX(date) FROM table GROUP BY id_company ORDER BY MAX(date) DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question