简体   繁体   中英

Convert LEFT OUTER JOIN to only use subqueries and annotations

Is it possible to convert a LEFT OUTER JOIN to use only queries and subqueries with annotations?

I have a complex query like this that I cannot use LEFT OUTER JOINS and need to somehow do without:

select count(cua.a),cua.b, w.c, cp.p
from cua
LEFT OUTER JOIN cp on cp.id=cua.b
JOIN cu on cu.id=cua.a
LEFT OUTER JOIN w on w.id = cu.w_id
where cp.id=1
and (date IS NULL or date > NOW())
group by cua.b, cp.p, w.c, w.name;

If I can just figure out how to do left outer joins with subqueries and other function then I think I can figure out the whole thing.

Any help or direction is appreciated.

EDIT: added table names (Xy)

This:

where cp.id=1

filters the results of your query in such a way that all the unmatched rows of the table cp are rejected, so:

LEFT JOIN cp 

is equivalent to:

INNER JOIN cp

Also this:

group by cua.b, cp.p, w.c, w.name

uses 2 columns of the table w , so unless you want groups with null s for these 2 columns, this:

LEFT OUTER JOIN w

is equivalent to:

INNER JOIN w

So I guess there is no need for subqueries because you can rewrite safely your query with only INNER joins:

SELECT count(cua.a),cua.b, w.c, cp.p
FROM cua
INNER JOIN cp ON cp.id=cua.b
INNER JOIN cu ON cu.id=cua.a
INNER JOIN w ON w.id = cu.w_id
WHERE cp.id=1
AND (date IS NULL OR date > NOW())
GROUP BY cua.b, cp.p, w.c, w.name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM