我是laravel的新人。我的查询是我需要从逗号分隔字段中找出值。
这是我的表:
tags_value
╔════╦══════════════╗
║ id ║ tags ║
╠════╬══════════════╣
║ 1 ║ css,html,php ║
║ 2 ║ php,java,css ║
║ 3 ║ java,c++,ios ║
╚════╩══════════════╝
这是我的SQL查询:
$query = DB::table('tags_value')
->whereRaw(FIND_IN_SET('css', Tags))
->get();
但它不起作用。
请帮我解决这个问题。 提前谢谢。
答案 0 :(得分:11)
您需要使用引号转义FIND_IN_SET()
的呼叫:
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(css,Tags)')
->get();
如果您想参数化在FIND_IN_SET
中搜索的列,那么您可以尝试这样的事情:
$colname = 'css'
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(?,Tags)', [$colname])
->get();
答案 1 :(得分:4)
试试这个:
->whereRaw("FIND_IN_SET('css',tags)")
答案 2 :(得分:1)
DB::table('tags_value')->whereRaw("find_in_set('php',tags)")....
请注意,将 find_in_set()
与 Laravel Eloquent 一起使用时 .find_in_set('.$tags.',columnName)
。有时我们会混淆我们需要在何处提供 variable 和 db 列名。
答案 3 :(得分:0)
如何在 Laravel 中使用 find in set 将动态值传递给 eloquent。 100% 工作。
$restaurants = Restaurant::whereRaw('FIND_IN_SET("'.$request->code.'",restaurant_code)')->get();