user466061 user466061 - 29 days ago 8
MySQL Question

MySql WHRE 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 its 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. Please provide me a good solution.

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

Answer

This query should solve your problems:

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