I try to ORDER 2 UNIONed queries. Running this:
SELECT b.id
FROM book.book b
WHERE title ILIKE '%something%'
UNION
SELECT b.id
FROM book.book b
JOIN book.book_person bp
ON bp.bookID = b.id
JOIN person p
ON p.id = bp.personID
WHERE lastname ILIKE '%something%'
ORDER BY b.title ASC, b.year DESC, b.volume ASC
gives me error:
ERROR: 42P01: missing FROM-clause entry for table "b"
LINE 12: ORDER BY b.title ASC, b.year DESC, b.volume ASC
^
LOCATION: errorMissingRTE, parse_relation.c:3140
Without ORDER-clause it works fine. And it works fine when I include cols I want to ordered by:
SELECT b.id, b.title, b.year, b.volume
FROM book.book b
WHERE title ILIKE '%something%'
UNION
SELECT b.id, b.title, b.year, b.volume
FROM book.book b
JOIN book.book_person bp
ON bp.bookID = b.id
JOIN person p
ON p.id = bp.personID
WHERE lastname ILIKE '%something%'
ORDER BY "title" ASC, "year" DESC, "volume" ASC
Is there better way to order UNIONed queris than include more columns?