简体   繁体   中英

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:

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. 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. Therefore, you may get different result when adding more fields to same MATCH clause. 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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