【发布时间】:2016-05-20 06:57:46
【问题描述】:
我想从表格中搜索特定单词而不是字母。现在我正在使用like,但它不像预期的那样:
// Category Table
-----------------------------------------------------
| id | amount | updated_at |
+----------+--------------------+-------------------+
| 1 | Bread |2016-02-10 13:17:29|
| 2 | Bread Loaf |2016-02-10 13:17:29|
| 3 | Bread1 |2016-02-10 13:17:29|
+----------+--------------------+-------------------+
例如我搜索“面包”这个词:
$words = "bread";
$query = Category::where(function ($query) use ($words) {
$query->where('name', 'like', '%' . $words . '%');
})->get();
结果:所有的面包都出来了。
我期望的是“Bread1”没有得到查询。只有面包和面包。
我应该在查询中添加什么?
【问题讨论】:
-
你可能想试试这个 SELECT * FROM products WHERE product_name RLIKE "[[:<:>:]]"; Same Question Here
-
基本上这被评估为
WHERE name LIKE '%bread%',这意味着它将匹配任何包含面包的东西。它不会将其评估为一个词。如果您只是添加了空格,它将不会匹配以面包开头或结尾的任何内容。 John Roca 用正则表达式给出了一个很好的解决方案。 -
@JohnRoca 谢谢。它工作
-
@SSuhat 你可以将我的答案标记为正确!