简体   繁体   English

用UNION和LEFT JOIN选择

[英]SELECT with UNION AND LEFT JOIN

Hi guys i've been trying this sql search query to my website search facility it supposed to search and order by relevance. 嗨,大家好,我一直在尝试对我的网站搜索工具进行此sql搜索查询,它应该按相关性进行搜索和排序。 It is working without the left join and I can't get it to work with left join. 它在没有左联接的情况下工作,而我无法在左联接的情况下工作。 Here is the SQL query. 这是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

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. 您名为“ bl_bands”的子查询没有bl_bands.id,因为所有的联合联接都不包括bl_bands表。 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. 看起来您可能已经复制/粘贴了一些SELECT语句/列名,但没有添加需要获得结果的联接。

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. 该查询包括许多以bl_bands前缀的字段,但是该表未包含在from语句或联接中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM