简体   繁体   English

LEFT OUTER JOIN保留第一个表使用的所有匹配结果,而第二个表仅使用

[英]LEFT OUTER JOIN keep all matched results from first table use where just for second table

When I try to execute the mysql query below here I won't return all the record id's giving in the WHERE clause. 当我尝试在此处执行以下mysql查询时,我不会在WHERE子句中返回所有记录ID。

What I am trying to do: 我正在尝试做的是:

  1. Get all result records by id (IN) 通过ID(IN)获取所有结果记录
  2. JOIN the second table (websites_thumbs) By website_salt of Table 1 record result JOIN第二个表(websites_thumbs)由表1的website_salt记录结果

    By salt of the record from table 1 but also a salt from the parent site ($parent_salt) 按表1中记录的盐,也按父网站中的盐($ parent_salt)

  3. SUM votes and return in record result (if no result in the second table is found return 0) SUM投票并返回记录结果(如果在第二个表中未找到结果,则返回0)

ID 119 and 250 are having voting results in the second table, the others doesn't. ID 119和ID 250在第二张表中具有投票结果,其他人则没有。 The problem I got is when I execute the query it returns just the records 1,119 & 250 the other id's are existing but they are not returned? 我遇到的问题是,当我执行查询时,它仅返回记录1,119和250,而其他ID已存在,但未返回?

The query: 查询:

// VxZQ85cByb98wUx0f3 => parent salt coming from another query (page data query) // VxZQ85cByb98wUx0f3 =>来自另一个查询的母盐(页面数据查询)

SELECT 
    SUM(CASE t.thumb when 1 then 1 else 0 end) as thumbs_up,
    SUM(CASE t.thumb when -1 then 1 else 0 end) as thumbs_down,
    w.*, t.*
FROM websites as w
  LEFT OUTER JOIN websites_thumbs AS t 
  ON t.similar_website_salt = w.salt AND t.website_salt = 'VxZQ85cByb98wUx0f3' 
WHERE w.id IN ('1', '20', '31', '4', '199', '250', '633953')
GROUP BY t.similar_website_salt

first table 第一张桌子

在此处输入图片说明

second table 第二张桌子 在此处输入图片说明

Executing result 执行结果

在此处输入图片说明

Hope someone can help me with this on! 希望有人可以帮助我!

Thanks 谢谢

Instead of LEFT OUTER JOIN try INNER JOIN . 代替LEFT OUTER JOIN尝试INNER JOIN Also, don't use ' in the IN clause. 另外,请勿在IN子句中使用' Lastly, I changed the order of your JOIN just because I'm picky :) 最后,只是因为我很挑剔,所以我更改了您的JOIN顺序:

SELECT 
    SUM(CASE t.thumb when 1 then 1 else 0 end) as thumbs_up,
    SUM(CASE t.thumb when -1 then 1 else 0 end) as thumbs_down,
    w.*, t.*
FROM websites as w
  INNER JOIN websites_thumbs AS t 
  ON w.salt = t.similar_website_salt AND t.website_salt = 'VxZQ85cByb98wUx0f3' 
WHERE w.id IN (1, 20, 31, 4, 199, 250, 633953)
GROUP BY t.similar_website_salt

It's kind of hard to say without seeing table schema, so if you want to throw some of that up there it would be helpful. 不查看表模式很难说,因此,如果您想将其中的一些内容扔掉,将会很有帮助。

Your use of GROUP BY is eliminating any rows with a null similar_website_salt . 您对GROUP BY使用将消除所有具有similar_website_salt为空的similar_website_salt You need to change the null value to something else (eg empty string), and then group by that. 您需要将null值更改为其他值(例如,空字符串),然后按该值分组。 Note there are two lines with the COALESCE addition: 请注意,添加了COALESCE有两行:

SELECT 
    SUM(CASE t.thumb when 1 then 1 else 0 end) as thumbs_up,
    SUM(CASE t.thumb when -1 then 1 else 0 end) as thumbs_down,
    w.*, t.*,

    COALESCE(t.similar_website_salt, '')

FROM websites as w
  LEFT OUTER JOIN websites_thumbs AS t 
  ON t.similar_website_salt = w.salt AND t.website_salt = 'VxZQ85cByb98wUx0f3' 
WHERE w.id IN ('1', '20', '31', '4', '199', '250', '633953')

GROUP BY COALESCE(t.similar_website_salt, '')

Not all records are having a sub record in the second table so we can't GROUP BY t.similar_website_salt and keep records in the result set without this value. 并非所有记录在第二个表中都有子记录,因此我们不能GROUP BY t.similar_website_salt并将记录保留在结果集中而没有该值。

SELECT 
    SUM(CASE t.thumb when 1 then 1 else 0 end) as thumbs_up,
    SUM(CASE t.thumb when -1 then 1 else 0 end) as thumbs_down,
    w.*, t.*
FROM websites as w
  LEFT OUTER JOIN websites_thumbs AS t 
  ON t.similar_website_salt = w.salt AND t.website_salt = 'VxZQ85cByb98wUx0f3' 
WHERE w.id IN ('1', '20', '31', '4', '199', '250', '633953')
//GROUP BY t.similar_website_salt
GROUP BY w.id <-- this does the trick

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

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