SELECT * FROM ( SELECT @homebranch := <> COLLATE utf8_unicode_ci AS barcode, @ccode := <> COLLATE utf8_unicode_ci AS branch, @lastcheckout := <> COLLATE utf8_unicode_ci AS itemcallnumber, 0 AS title, 0 AS biblionumber, 0 AS author ) AS `set variables` WHERE 0 = 1 UNION SELECT items.barcode AS barcode, items.homebranch AS branch, items.itemcallnumber AS itemcallnumber, biblio.title AS title, biblio.biblionumber AS biblionumber, biblio.author AS author FROM items LEFT JOIN biblio USING (biblionumber) LEFT JOIN ( SELECT itemnumber, max( issuedate ) AS issuedate FROM ( SELECT * FROM issues UNION SELECT * FROM old_issues ) AS all_issues GROUP BY itemnumber ) AS last_checkout USING (itemnumber) WHERE items.homebranch = @homebranch AND items.ccode = @ccode AND date( last_checkout.issuedate ) < @lastcheckout