tp6 find_in_set
时间: 2023-08-15 13:10:38 浏览: 135
In ThinkPHP 6 (TP6), the `find_in_set` function is used to search for a value within a comma-separated list of values. It returns the position of the value if found, otherwise, it returns 0. Here's an example usage:
```php
use think\db\Raw;
// Assuming you have a model named "User" and a field named "skills" containing a comma-separated list of skills
$skill = 'PHP';
// Query for users with the skill 'PHP' in their skills list
$users = User::where(new Raw("FIND_IN_SET('{$skill}', skills)"))->select();
// You can also use it in the query builder
$users = Db::table('user')->where(new Raw("FIND_IN_SET('{$skill}', skills)"))->select();
// Loop through the results
foreach ($users as $user) {
// Do something with each user
}
```
Make sure to properly sanitize or bind the value if it comes from user input to prevent SQL injection attacks.
阅读全文