I AM FULLY AWARE OF THIS QUESTION AND ANSWER: https://stackoverflow.com/a/4796911/2307520 BUT THIS IS A BIT DIFFERENT PROBLEM!
Yes, I understand how they simulated simple FULL OUTER JOIN in MySQL using union and two left joins, but my situation is a bit more complicated (3 tables instead of just 2) and I struggle finding a way how to modify that solution for my needs.
This is simplified database schema:
CREATE TABLE item (`i_id` int, `i_name` varchar(70), b_id int null , s_id int null , PRIMARY KEY (i_id));
CREATE TABLE box (`b_id` int, `b_name` varchar(70), s_id int null , PRIMARY KEY (b_id) );
CREATE TABLE shelf (`s_id` int, `s_name` varchar(70) , PRIMARY KEY (s_id));
INSERT INTO shelf VALUES(1,'shelf1');
INSERT INTO shelf VALUES(2,'shelf2');
INSERT INTO shelf VALUES(3,'empty shelf');
INSERT INTO box VALUES(1,'box in s1 with item in it',1);
INSERT INTO box VALUES(2,'box without shelf',NULL);
INSERT INTO box VALUES(3,'empty box',2);
INSERT INTO item VALUES(1,'item a',1,NULL);
INSERT INTO item VALUES(2,'item b',1,NULL);
INSERT INTO item VALUES(3,'item c',2,NULL);
INSERT INTO item VALUES(4,'item in a shelf',NULL,1);
INSERT INTO item VALUES(5,'item without location',NULL,NULL);
item can be either in a box, standalone in a shelf, or in neither (not defined). Box can be in a shelf or not defined.
Ideally, I would use SQL query like this: (If I had FULL OUTER JOIN in MySQL)
SELECT *,coalesce(item.b_id,box.b_id) as b_id,coalesce(item.s_id,box.s_id,shelf.s_id) as s_id
FROM item
FULL OUTER JOIN box ON box.b_id=item.i_id
FULL OUTER JOIN shelf ON (shelf.s_id=item.s_id) OR (shelf.s_id=box.s_id)
WHERE ...
That would list all entries for certain criteria (like to see in which shelf a box is if any and all items in it, or anything else)