I have a little issue with the ORDER BY statement.
Here is my query (sorry I know is a little bit long, but I alredy cut a lot of the code)
(SELECT AssAttrezzi.ID, AssBombole.matricola, CONCAT(AssBombole.matricola, ' - ', Bombole.nome) AS Descrizione
FROM AssAttrezzi INNER JOIN
AssBombole
ON AssBombole.matricola = AssAttrezzi.attrezzoID INNER JOIN
Bombole
ON Bombole.ID = AssBombole.tipoBombolaID
WHERE AssAttrezzi.cantiereID=1
) UNION
(SELECT AssAttrezzi.ID, Saldatrici.matricola,CONCAT(Saldatrici.matricola, ' - ', Saldatrici.nome) AS Descrizione
FROM AssAttrezzi INNER JOIN
Saldatrici
ON Saldatrici.matricola = AssAttrezzi.attrezzoID
WHERE AssAttrezzi.cantiereID = 1
) UNION
(SELECT AssAttrezzi.ID, AssBanchiGlad.idAtrezzo, CONCAT(AssBanchiGlad.idAtrezzo, ' - ', BanchiGladiator.nome) AS Descrizione
FROM AssAttrezzi INNER JOIN
AssBanchiGlad
ON AssBanchiGlad.idAtrezzo = AssAttrezzi.attrezzoID INNER JOIN
BanchiGladiator
ON BanchiGladiator.ID = AssBanchiGlad.bancoID
WHERE AssAttrezzi.cantiereID = 1
) ORDER BY LEN(matricola), matricola
The problem is when I put ORDER BY at the end of each SELECT It gives me error:
Incorrect syntax near UNION
But when I try to put the ORDER BY at the end of all it gives me another error:
If the ORDER BY statement includes the UNION, INTERSECT or EXCEPT operator, the elements of the instruction must be specified in the selection list.
What? I would like to order by *.matricola basically matricola for me is the important one.