I am working on a game inventory management system and would like to show the wish list of the replenishment owner and the calculation of customer purchase orders for each game in one table. I wrote a query that I thought worked, but then I noticed that it actually omits any games for which there are reservations, but which are not originally on the restock wish list. Request below:
SELECT rwl.*, g.gameName, coalesce(payYes, 0) payYes, coalesce(payNo, 0) payNo FROM RestockWishList AS rwl, Games AS g LEFT JOIN (SELECT gameID, COUNT(if(prepaid='Yes', 1, NULL)) payYes, COUNT(if(prepaid='No', 1, NULL)) payNo FROM ReservationsBuy GROUP BY gameID) AS res ON res.gameID = g.gameID WHERE rwl.gameID = g.gameID;
Query results: GameID, quantity, GameName, payYes, payNo
1, 4, Castle for all seasons, 0, 0
2, 2, A few acres of snow, 0, 0
18, 4, Alhambra, 0, 0
54, 2, Big Boggli, 2, 0
Apparently, the solution to this problem is to use FULL OUTER JOIN instead of LEFT JOIN, but MySQL does not support this function. I spent hours trying to translate it into a UNION structure, but can't make it work correctly. It is as close as mine:
SELECT rwl.*, res.gameID, res.payYes, res.payNo FROM RestockWishList rwl LEFT JOIN (SELECT gameID, COUNT(if(prepaid='Yes', 1, NULL)) payYes, COUNT(if(prepaid='No', 1, NULL)) payNo FROM ReservationsBuy GROUP BY gameID) AS res ON res.gameID = rwl.gameID UNION SELECT rwl.*, res.gameID, COUNT(if(prepaid='Yes', 1, NULL)) payYes, COUNT(if(prepaid='No', 1, NULL)) payNo FROM ReservationsBuy res LEFT JOIN RestockWishList rwl ON rwl.gameID = res.gameID;
Query results: gameID, quantity, gameID, payYes, payNo
1, 4, NULL, NULL, NULL
2, 2, NULL, NULL, NULL
18, 4, NULL, NULL, NULL
54, 2, 54, 2, 0
NULL, NULL, 30, 3, 1
(Sorry, I donβt know how to nicely format query table results in StackOverflow.)
I want the query to be displayed, as I originally wrote it, just with missing values ββfrom ReservationsBuy. Specific help please?
Tables:
CREATE TABLE IF NOT EXISTS RestockWishList ( gameID INT(6), quantity INT(3) NOT NULL, PRIMARY KEY (gameID), FOREIGN KEY (gameID) REFERENCES Games(gameID) ON UPDATE CASCADE ON DELETE CASCADE); CREATE TABLE IF NOT EXISTS ReservationsBuy ( gameID INT(6), customerEmail VARCHAR(25) NOT NULL, customerName VARCHAR(25) NOT NULL, dateReserved DATETIME NOT NULL,
Sample data: RestockWishList:
GameID Value
14
2, 2
18, 4
54, 2
ReservationsBuy:
GameID, customerEmail, Customer Name, dateReserved, Purchase Date, dateClaimed, prepayment
30, wonder@woman.com , Diana, 2015-04-24 14:46:05, ZERO, ZERO, Yes
54, boggie@marsh.com , Cart, 2015-04-24 14:43:32, ZERO, ZERO, Yes
54, manny@second.com , Manny, 2015-04-27 19:48:22, ZERO, ZERO, Yes
43, old@mom.com , grandmother, 2015-04-23 22:32:03, ZERO, ZERO, No
Expected Result: gameID, quantity, gameName, payYes, payNo
1, 4, Castle for all seasons, 0, 0
2, 2, A A few acres of snow, 0, 0
18, 4, Alhambra, 0, 0
30, 0, Arkham Horror, 1, 0
43, 0, Bananagrams, 0, 1
54, 2, Big Boggle, 2, 0
(The game table is not particularly important for this query. Only the relevance is that both ReservationsBuy and RestockWishList are associated with the Game game).