I am using MySql and running the following queries.
(select CONCAT(RESOURCE, " AMB:", AMB) AS REC_1 from db.CRE
where CONTENT = '183' and LEVEL = '99'
EXCEPT
select CONCAT(RESOURCE, " AMB:", AMB) AS REC_1 from db.CRE
where CONTENT = '182' and LEVEL = '99')
(select CONCAT(RESOURCE, " AMB:", AMB) AS REC_SAME from db.CRE
where CONTENT = '183' and LEVEL = '99'
INTERSECT
select CONCAT(RESOURCE, " AMB:", AMB) AS REC_SAME from db.CRE
where CONTENT = '182' and LEVEL = '99')
(select CONCAT(RESOURCE, " AMB:", AMB) AS REC_2 from db.CRE
where CONTENT = '182' and LEVEL = '99'
EXCEPT
select CONCAT(RESOURCE, " AMB:", AMB) AS REC_2 from db.CRE
where CONTENT = '183' and LEVEL = '99')
The subqueries are always the same but the set operation changes. All of these queries work on their own. The first tells me the feature unique to subquery a the second what both share and the third what is unique to subquery b. The issue is that I would like to get all three of these returned at the same time. One column being REC_1 another REC_SAME another REC_2.
I have tried using UNIONS to join the but this fails if any of them are null. I have also tried using select without a from but it also didn’t work.
The results i would expect to be with the following structure.In which rec1 would contain the results of the first query , rec_same of the intersect query and rec_2 of the last query.
Rec1|Rec_same|Rec2
3
Answers
AS MySQL doesn’t support
FULL OUTER JOIN
you have to build it by handYou can add rec_type(‘rec_1’, ‘rec_same’,’rec_2′) columns in all 3 subqueries, and then perform a union. At the end, use group by and sum function to exact which rec is rec_1/rec_same/rec_2. See the example query:
You can do:
Result:
See running example at db<>fiddle.