Reputation: 493
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.
Upvotes: 1
Views: 47814
Reputation: 1449
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:
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
JOIN bl_bands ON bl_bands.id = bl_albums.bandid
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
JOIN bl_bands ON bl_bands.id = bl_albums.bandid
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
JOIN bl_bands ON bl_bands.id = bl_albums.bandid
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
JOIN bl_bands ON bl_bands.id = bl_albums.bandid
WHERE bl_albums.name like '%Camera%'
) bl_bands ON bl_albums.bandid = bl_bands.id
GROUP BY bl_albums.name
ORDER BY relevance desc
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.
Upvotes: 3
Reputation: 70523
I don't believe this query ever worked. The inner queries make no sense. For example
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'
This query includes many fields pre-fixed with bl_bands, but that table is not included in the from statement or a join.
Upvotes: 0