Hi guys i’ve been trying this sql search query to my website search facility it supposed to search and order by relevance. It is working without the left join and I can’t get it to work with left join. Here is the SQL query.
SELECT bl_albums.*, bl_bands.name as bandname, bl_bands.id as bandid,
bl_bands.bandpage as bandpage, sum(relevance)
FROM bl_albums
LEFT JOIN(
SELECT bl_albums.*, bl_bands.name as bandname, bl_bands.id as bandid,
bl_bands.bandpage as bandpage,10 AS relevance
FROM bl_albums
WHERE bl_albums.name like 'Camera'
UNION
SELECT bl_albums.*, bl_bands.name as bandname, bl_bands.id as bandid,
bl_bands.bandpage as bandpage, 7 AS relevance
FROM bl_albums
WHERE bl_albums.name like 'Camera%'
UNION
SELECT bl_albums.*, bl_bands.name as bandname, bl_bands.id as bandid,
bl_bands.bandpage as bandpage, 5 AS relevance
FROM bl_albums
WHERE bl_albums.name like '%Camera'
UNION
SELECT bl_albums.*, bl_bands.name as bandname, bl_bands.id as bandid,
bl_bands.bandpage as bandpage, 2 AS relevance
FROM bl_albums
WHERE bl_albums.name like '%Camera%'
) bl_bands on bl_albums.bandid = bl_bands.id
GROUP BY bl_albums.name
ORDER BY relevance desc
all table names are correct and all column names are correct.
Your subquery that you’ve named ‘bl_bands’ doesn’t have bl_bands.id because all the union joins don’t include the bl_bands table. Try adding joins to each union, if my assumption on all your data is correct:
It looks like you might have copied/pasted some SELECT statements/column names but didn’t add the joining in that you needed to get the results.