Search code examples
phpmysqlsql-like

MySql WHERE LIKE before particular character?


I have to search for a particular word from two columns but in one column I have to search before * character. so I used SELECT * FROM table_name WHERE status = 'Enable' AND topic LIKE '%car%' OR category LIKE '%car%' now the problem is it's returning beauty column also because it's category column has the word care, now I have to make it if the word contains before the first * it has to return.

+--------+---------------------------------------------------------------------------+---------+
| topic  | category                                                                  | status  |
+--------+---------------------------------------------------------------------------+---------+
| car    | cars & vechicles*cars*                                                    | Enabled |
+--------+---------------------------------------------------------------------------+---------+
| beauty | Fashion, Health & Beauty*Health and Beauty Products*Body Care / Skin Care | Enabled |
+--------+---------------------------------------------------------------------------+---------+

Solution

  • This query should solve your problems:

    SELECT * FROM table_name WHERE 
    status = 'Enable' AND topic LIKE '%car%' OR SUBSTRING_INDEX(category,'*',1) LIKE '%car%'