在 Laravel 中找到?例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35594450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find in set in laravel ? example
提问by Ravi Kadia
I am new in laravel. My query is i need to find out value from comma separated field.
我是laravel的新手。我的查询是我需要从逗号分隔的字段中找出值。
Here is my table:
这是我的表:
tags_value
标签值
╔════╦══════════════╗
║ id ║ tags ║
╠════╬══════════════╣
║ 1 ║ css,html,php ║
║ 2 ║ php,java,css ║
║ 3 ║ java,c++,ios ║
╚════╩══════════════╝
This is my SQL query:
这是我的 SQL 查询:
$query = DB::table('tags_value')
->whereRaw(FIND_IN_SET('css', Tags))
->get();
but it's not working.
但它不起作用。
Please help me to solve this problem. Thanks in advance.
请帮我解决这个问题。提前致谢。
回答by Tim Biegeleisen
You need to escape the call to FIND_IN_SET()
using quotes:
您需要将调用转义为FIND_IN_SET()
使用引号:
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(css,Tags)')
->get();
If you want to parameterize the column for which you search in FIND_IN_SET
, then you can try something like this:
如果你想参数化你搜索的列FIND_IN_SET
,那么你可以尝试这样的事情:
$colname = 'css'
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(?,Tags)', [$colname])
->get();
回答by Mr. Engineer
Try this :
尝试这个 :
->whereRaw("FIND_IN_SET('css',tags)")