MySql order clause ORDER BY

0

I have to make a series of selects to get information, as we always do.

The thing is that it would be much easier to sort them using first ORDER BY after UNION .

I leave the consultations here

select CAST(COUNT(*) AS CHAR(10)) -- Valor1
from tablaA
where fecha = fechaactiva

Union

select time(llamada) -- Valor2
from TablaB
order by calldate desc
limit 1

Union

SELECT campo -- Valor3
FROM TablaA
limit 1

In principle this could not be done, I understand that this is wrong and the ORDER BY should go to the end, or that's how I learned it, how could I just sort the data of a SELECT?

This is the error that the interpreter of BBDD I use.

  

SQL error (1221) : Incorrect usage of UNION and ORDER BY

    
asked by Aritzbn 26.04.2018 в 14:01
source

2 answers

1

Think that you can not use a clause ORDER BY partially in any% common%, the order applies to the whole query, although in your case use SELECT the final result is still a single query. The UNION is always resolved by the engine at the end of everything, when you have already solved the rest of the sentences, the other limitation you have is that you can not sort by a column that only exists in a part of ORDER .

Also, a secondary fact, which is not worth commenting on: it is that the engine does not guarantee a natural order when a UNION is not specified, that is to say that without this clause, we will never be guaranteed that the rows have the same order always.

To do what you want, you can try:

Using subqueries

In this way the engine "understands" that it must first process a subquery, which in this case can have a ORDER . For example:

SELECT  Campo
        FROM (SELECT Campo, calldate  FROM Tabla1 ORDER BY calldate) T
UNION
SELECT Campo FROM Tabla2
UNION
SELECT Campo FROM Tabla3;

However, whether or not it works depends on the engine, so before I said, since the final query does not have an order, do we have a guarantee that the data set of the first ORDER will always be ordered in the same way? ?

Adding the field to the query

SELECT calldate, Campo FROM Tabla1
UNION
SELECT NULL, Campo FROM Tabla2
UNION
SELECT NULL, Campo FROM Tabla3
       ORDER BY 1 DESC

This is the appropriate form, we must add the column to be sorted in the first query and return select in the position of said column in the other queries, in this way we can apply the NULL at the end and we end up insuring that the final result will have the rows of the first ORDER at the beginning and sorted by SELECT . (In a calldate the ORDER goes first, that's why we use NULL )

    
answered by 26.04.2018 / 15:52
source
1

For your query and as you investigate, you could do it in the following way. But first some clarifications regarding the use of these.

UNION:
It is used when all the selections have the same number of columns, they must be of the same type and the same order.

UNION ALL:
Select all the default values and allow duplicate values.

UNION DISTINCT:
Remove duplicate rows and only return single rows.

And the way that occurs to make your queries would be as follows.

(select CAST(COUNT(*) AS CHAR(10)) -- Valor1
from tablaA
where fecha = fechaactiva)
UNION ALL
(select time(llamada) -- Valor2
from TablaB
limit 1)
UNION ALL
(SELECT campo -- Valor3
FROM TablaA
limit 1)order by calldate desc;
  

PS: If your version of mysql is 5.7 up, it has to be with   parentheses.

    
answered by 26.04.2018 в 15:05