I’ve 2 queries that must be run on a desk.
[SELECT *, 1 as query_cr_0 FROM invoices LEFT OUTER JOIN customers ON customers.id = invoices.customer_id WHERE (customers.id IN (64, 66, 63, 62, 65)), SELECT *, 5 as query_cr_1 FROM invoices WHERE (invoices.state IN ('ready', 'out_for_delivery', 'delivered', 'closed')]
Now i wish to fetch all of the invoices that fulfill both of the queries.
My method was to make use of UNION however as the primary question is utilizing left outer be part of i’m not in a position to make use of it.
ERROR: every UNION question should have the identical variety of columns
My second method was to make use of one thing like this:
choose invoices.id from invoices the place id in (SELECT invoices.id, 1 as query_cr_0 FROM invoices LEFT OUTER JOIN clients ON clients.id = invoices.customer_id WHERE (clients.id IN (64, 66, 63, 62, 65))) OR id in (SELECT invoices.id, 5 as query_cr_1 FROM invoices WHERE (invoices.state IN ('prepared', 'out_for_delivery', 'delivered', 'closed')))
However that is throwing an error
subquery has too many columns
My objective is to fetch the invoices with query_cr_0 and query_cr_1 worth. For ex: An bill is likely to be satisfying each the situations so in that case each query_cr_0 and query_cr_1 must be current. If an bill satisfies solely one of many queries corresponding query_cr should be current.
Easy methods to remedy this difficulty?