Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I improve my LIKE with JOIN search in mysql?

I currently have a search that looks like this:

SELECT s.ID 
    FROM Shoe s
        INNER JOIN Account a
            ON s.UserID = a.ID
        WHERE s.Publish='1' AND
            (s.Brand LIKE '%$Search%'
             OR s.Name LIKE '%$Search%'
             OR s.PrimaryColor LIKE '%$Search%'
             OR a.User LIKE '%$Search%') 
        ORDER BY s.ID DESC
LIMIT $offset, $rowsPerPage"

This works fine when I do a search such as "Blue" or "Nikes", but if I do a search such as "Blue Nikes" nothing returns. Should I use FULLTEXT? How can I improve this? I want to be able to search all columns that may relate to the search variable.

like image 479
SReca Avatar asked Oct 12 '12 18:10

SReca


People also ask

How do I make my Joins faster?

1. Always reduce the data before any joins as much possible. 2. When joining, make sure smaller tables are on the left side of join syntax, which makes this data set to be in memory / broadcasted to all the vertica nodes and makes join faster.

Why we use LEFT JOIN in MySQL?

Note: The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).

What is LEFT JOIN in MySQL?

The Left Join in MySQL is used to query records from multiple tables. This clause is similar to the Inner Join clause that can be used with a SELECT statement immediately after the FROM keyword.


1 Answers

So after messing around and testing different things, I came up with this:

"FROM Shoe AS s
    LEFT JOIN Accounts AS a ON s.UserID = a.ID
    WHERE (MATCH (s.Brand, s.Name, s.PrimaryColor AGAINST('$Search' IN BOOLEAN MODE)
        OR MATCH (a.User) AGAINST('$Search' IN BOOLEAN MODE))
            AND s.Publish='1'
    ORDER BY s.ID DESC"

This seems to fix my issue that I mentioned above, I can now do a search such as "Blue Nike" and all items related to blue & nike will show up. Not sure if this is the most efficient way to go about it, but it does work.

like image 133
SReca Avatar answered Sep 18 '22 13:09

SReca