Working sample using one Table
SELECT t.* FROM (
SELECT
TITLE.name,
(TITLE.value-TITLE.msp) AS Lower,
(TITLE.value+TITLE.msp) AS Upper,
(TITLE.value) AS Value
FROM TITLE
) t
WHERE 98 BETWEEN t.Lower AND t.Upper
ORDER BY ABS(98 - t.Value) ASC
LIMIT 5
Desired example working with 3 tables (Needs fixed/help)
SELECT t.* FROM (
SELECT
TITLE.name, ALBUM.year, GENRE.Type
(TITLE.value-TITLE.msp) AS Lower,
(TITLE.value+TITLE.msp) AS Upper,
(TITLE.value) AS Value
FROM TITLE, ALBUM, GENRE
) t
WHERE ALBUM.ID=GENRE.ID AND TITLE.ID=ALBUM.ID
AND 98 BETWEEN t.Lower AND t.Upper
ORDER BY ABS(98 - t.Value) ASC;
I get the following error:
ERROR 1054 (42S22): Unknown column ‘ALBUM.ID’ in ‘where clause’
This is logical: your subquery creates one table
t, and afterwords you try to refer to a tableALBUM. There is noALBUM, there is onlytThe quickest fix it to move the
WHEREclause for the JOIN where it belongs: in the subquery.The query itself ain’t that beautiful though… why not try this instead: