大家好,我一直在尝试这个sql搜索查询到我的网站搜索工具,它应该按相关性搜索和排序。它没有左连接工作,我无法使用左连接。这是SQL查询。
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
所有表名都正确,所有列名都正确。
答案 0 :(得分:3)
您命名为'bl_bands'的子查询没有bl_bands.id,因为所有联合连接都不包含bl_bands表。如果我对所有数据的假设是正确的,请尝试向每个联合添加联接:
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
看起来你可能已经复制/粘贴了一些SELECT语句/列名,但没有添加你需要获得结果的连接。
答案 1 :(得分:0)
我不相信这个查询有效。内部查询毫无意义。例如
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'
此查询包含许多使用bl_bands预先修复的字段,但该表未包含在from语句或连接中。