[英]search exact string exist within column mysql
I'm using below sql query for search values from db, but when I search 'study' it will returns the values 'caese-study', 'get-study-materials' as well. 我正在使用下面的sql查询来查询来自db的搜索值,但是当我搜索“ study”时,它也会返回值“ caese-study”,“ get-study-materials”。 How can I use a query to search exact contains withing string column?
如何使用查询来搜索确切的包含withing字符串列?
$names = 'study'; $ names ='study'; and the names column has values like comma separated, Ex: 'study, abc, new' so I need to search within that too
并且名称列具有类似逗号分隔的值,例如:“ study,abc,new”,因此我也需要在其中进行搜索
SELECT * FROM datatitle WHERE names LIKE '%$names %' ;
SELECT * FROM datatitle WHERE names regexp '(^|[[:space:]])$names([[:space:]]|$)';
I try with above two queries but didnt work as expect, pls advice? 我尝试了上述两个查询,但没有按预期工作,请咨询?
You should not be storing comma-separated values in a column. 您不应该在列中存储逗号分隔的值。 You should be using a junction/association table.
您应该使用联结/关联表。 You should fix the data model, if you can.
如果可以,您应该修复数据模型。
However, sometimes we cannot control other people's really bad decisions. 但是,有时我们无法控制其他人的错误决定。 MySQL has
find_in_set()
: MySQL具有
find_in_set()
:
SELECT dt.*
FROM datatitle dt
WHERE find_in_set(?, names) > 0;
Note that I have replaced the constant $names
with a parameter. 请注意,我已用参数替换了常量
$names
。 You should learn to use parameters to pass values into queries. 您应该学习使用参数将值传递到查询中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.