Ciao a tutti, ho un problema su una query con una union su due select
La struttura del DB (che non posso modificare) è la seguente:
TABLE_C
id
name
TABLE_D
id
tablec_id (FK)
label
TABLE_SD
id
tablec_id (FK)
label
e una terza tabella TABLE che ha come campi
id
tabled_id (FK)
tablesd_id (FK)
può succedere che ci in TABLE ci siano dati con solo TABLE_SD non nulla mentre non ci sono campi con TABLE_D non nulli.
A seconda dei campi di ricerca e order by ce ricevo potrei avere questa query:
select t.id deviceID
from TABLE t
left join TABLE_SD sd on t.tablesd_id =sd.id
left join TABLE_C c on sd.tablec_id=c.id
where c.name LIKE ('%ubb%')
union
select t.id deviceID
from TABLE trp
left join TABLE_D d on t.tabled_id =d.id
left join TABLE_C c on d.client_id=c.id
where c.name LIKE ('%ubb%')
order by d.label asc
in questo caso la tabella TABLE ha solo elementi collegati a TABLE_SD e non a TABLE_D e io vorrei che mi tornasse tutti gli elementi di TABLE ignorando (di fatto) l'orderby è possibile?
O si può in qualche modo intercettare il fatto che la seconda query non ha elementi ignorando quindi l'order_by?
La query mi da come errore Table 'd' from one of the SELECTs cannot be used in ORDER clause
Invece funziona questa query:
select t.id deviceID
from TABLE t
left join TABLE_D d on t.tabled_id =d.id
order by d.label asc
e mi torna tutti i dati di TABLE ignorando, di fatto, l'order by
Grazie a chi riuscirà a darmi supporto