简体   繁体   English

SQL LEFT JOIN / INNER JOIN不同的结果

[英]SQL LEFT JOIN/INNER JOIN different result

I don't know what I'm doing wrong but can't found solution for my statement. 我不知道自己在做什么错,但找不到解决方案。 Current statement is: 当前声明是:

SELECT tb1.RestaurantID, Title, City
FROM Restaurant AS tb1
LEFT JOIN RestaurantLunch AS tb2 ON tb1.RestaurantID = tb2.RestaurantID
WHERE (MATCH (tb1.Title, tb1.City) AGAINST ('+string1* +string2*' IN BOOLEAN MODE)) LIMIT 5;

Offcourse i would also like to search for string in other table(tb2) and i change my statement to: 当然,我也想在其他表(tb2)中搜索字符串,然后将语句更改为:

SELECT tb1.RestaurantID, Title, City
FROM Restaurant AS tb1
LEFT JOIN RestaurantLunch AS tb2 ON tb1.RestaurantID = tb2.RestaurantID
WHERE (MATCH (tb1.Title, tb1.City, tb2.Text) AGAINST ('+string1* +string2*' IN BOOLEAN MODE)) LIMIT 5;

After submit i get different result, which is not logic at all. 提交后,我得到不同的结果,这根本不是逻辑。 Why is that? 这是为什么?

When you do a left join and create a condition in where section to the table on right (tb2 in your case) this left join became a plain join with that condition. 当您执行左联接并在右表的where部分(在您的情况下为tb2)中创建条件时,此左联接变为具有该条件的普通联接。 That is why you are getting different results. 这就是为什么您获得不同结果的原因。

Try this way: 尝试这种方式:

SELECT tb1.RestaurantID, Title, City
  FROM Restaurant AS tb1
        LEFT JOIN RestaurantLunch AS tb2 
                  ON tb1.RestaurantID = tb2.RestaurantID
                     AND (MATCH (tb2.Text) 
                              AGAINST ('+string1* +string2*' IN BOOLEAN MODE))
 WHERE (MATCH (tb1.Title, tb1.City) 
            AGAINST ('+string1* +string2*' IN BOOLEAN MODE)) LIMIT 5;

The InnoDB tables do not allow searches over several fulltext indexes in the same MATCH clause. InnoDB表不允许在同一MATCH子句中搜索多个全文索引。 Therefore, you may get different result when adding more fields to same MATCH clause. 因此,将更多字段添加到同一MATCH子句时,您可能会得到不同的结果。 Here your fields do not all belong to the same table, therefore they are covered by different indexes. 在这里,您的字段并不全部属于同一表,因此它们被不同的索引覆盖。

I think you should split your search to use one single fulltext index per MATCH clause. 我认为您应该将搜索拆分为每个MATCH子句使用一个全文索引。 You could try 你可以试试

SELECT 
   tb1.RestaurantID, Title, City
FROM Restaurant AS tb1
LEFT JOIN RestaurantLunch AS tb2 ON tb1.RestaurantID = tb2.RestaurantID
WHERE (MATCH (tb1.Title, tb1.City) AGAINST ('+string1* +string2*' IN BOOLEAN MODE)) 
      OR (tb2.Text IS NOT NULL AND MATCH (tb2.Text) AGAINST ('+string1* +string2*' IN BOOLEAN MODE)) 
LIMIT 5;

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

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