MYSQL: sort by specific rows within SELECT

1

I've been trying to resolve this for about three days and found the answer nowhere.

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 the table I made quickly

SELECTS and GROUPS are working, it's just the ORDER that I can not solve, one of several queries that 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

Example well summarized in more detail (the original table is too big to paste here):

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


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

Output:

-------------------------------
Pizza     |  28  | 2017-02-23 |
Gato      |  25  | 2017-02-20 |
Bombom    |   0  | 2017-02-15 |
-------------------------------
    
asked by anonymous 04.03.2017 / 18:11

2 answers

1

You can use BETWEEN in the sort command without any problems.

A simplified example of how you could do:

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

Note that I am doing IF and putting values 1 and 50. You can put whatever 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.

    
06.03.2017 / 20:52
0

In MySQL everything is or turns table.

One thing you can do is grab the lines the way they are coming, such as 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
    
04.03.2017 / 19:48