MYSQL: sort by specific Rows within SELECT

Asked

Viewed 62 times

1

I’ve been trying to figure this out for three days and I haven’t found the answer anywhere

I have a table in the database where I need to do a SELECT on all products but they need to be sorted according to the average between date X and date X, a basic example of table q I did quickly

Exemplo da formação da tabela

SELECTS and GROUPS are working, it’s only ORDER that I can’t solve, one of several queries I tried unsuccessfully to organize the Rows:

SELECT product, time, date FROM graficos
ORDER BY time HAVING (date BETWEEN 2017-02-20 AND 2017-02-23) DESC

Well summarized example in more detail (the original table is too giant to paste here):

-------------------------------
 PRODUCT  | TIME |    DATE    |


Bombom    |  35  | 2017-02-15 |
Gato      |  25  | 2017-02-20 |
Pizza     |  28  | 2017-02-23 |
-------------------------------

Exit:

-------------------------------
Pizza     |  28  | 2017-02-23 |
Gato      |  25  | 2017-02-20 |
Bombom    |   0  | 2017-02-15 |
-------------------------------
  • Exemplify with table records. What are the records and how you need the output?

  • I just gave an update with the details you asked Murillo

2 answers

1


You can use a BETWEEN in the smoothly sorted command.

A simplified example of how I could do:

SELECT
*
FROM graficos
ORDER BY (
  IF(`date` BETWEEN '2017-02-20' AND '2017-02-23', 1, 50)
)

Note that I’m doing the IF and setting the values 1 and 50. You can put any value you want there, I used these just to illustrate. What you need to understand at this point is that the result of this expression will be used to sort the records.

0

In Mysql everything is or becomes a table.

One thing you can do is take the lines the way they’re coming, like a table, and sort that table that came.

EX:

SELECT PRODUCT,TIME,DATE  FROM ( SEU_SQL_QUE_FUNCIONA_MAS_NAO_ESTA_ORDENADO ) AS tabelatemp ORDER BY TIME DESC

Browser other questions tagged

You are not signed in. Login or sign up in order to post.