MySql在特定字符之前是什么样的?

时间:2016-11-03 18:10:42

标签: php mysql sql-like

我必须从两列中搜索特定单词,但在一列中我必须在*字符之前搜索。所以我现在使用SELECT * FROM table_name WHERE status = 'Enable' AND topic LIKE '%car%' OR category LIKE '%car%'问题是它返回了美容专栏,因为它的类别栏中有 car e这个词,现在我必须在第一个*之前包含该词回来。

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

2 个答案:

答案 0 :(得分:1)

此查询应解决您的问题:

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

答案 1 :(得分:0)

您可以添加"%*"对您的查询进行简单修改。像这样:

SELECT * FROM table_name WHERE status = 'Enable' AND topic LIKE '%car%' OR category LIKE '%car%*%'