当前位置:网站首页>Laravel8中的find_in_set、upsert的使用方法
Laravel8中的find_in_set、upsert的使用方法
2022-07-02 06:23:00 【夜空の雪風】
前言
在laravel8中有时候需要使用FIND_IN_SET原生MySql语句,精准查询特殊字符串是否存在指定字符串中解决like无法精准匹配问题。
例如:type字段,type = 1,11,111。type = 2,22,222。采用like模糊查询的时候无法做到精确匹配,就需要用到FIND_IN_SET精确匹配查询。
用法
//DB::table('表名')->whereRaw('FIND_IN_SET(?,字段名)',[需要查询的字符串])->get();
$type = 1;
//代码演示
$data1 = DB::table('file')->whereRaw('FIND_IN_SET(?,type)',[$type])->get();
$data2 = FileModel::whereRaw('FIND_IN_SET(?,type)',[$type])->get();
前言
在解决向数据库表中一次性插入大量数据的时候可以采用upsert方法。在这里放上Laravel8官方文档中的说明。
如果你想在单次查询中执行多个 upsert,那么应该使用 upsert 方法。该方法的第一个参数是由要插入或更新的值组成,而第二个参数列出相应表中惟一标识记录的列,该方法的第三个也是最后一个参数是一个列数组,即如果数据库中已经存在匹配的记录,应该被更新的列。如果模型上启用了时间戳,upsert 方法将自动设置 created_at 和 updated_at 时间戳。
App\Models\Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);
用法
实际使用:
$insert_data = [
['name' => '文件1', 'use' => '质量', 'type' => 1],
['name' => '文件2', 'use' => '安全', 'type' => 2],
['name' => '文件3', 'use' => '进度', 'type' => 3],
];
FileModel::upsert($insert_data, ['name', 'use', 'type']);//这里写入数据表的字段值
边栏推荐
- Pytest (3) parameterize
- Differences between ts and JS
- Sqli labs customs clearance summary-page3
- SQL injection closure judgment
- Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
- Self study table Au
- 如何调试微信内置浏览器应用(企业号、公众号、订阅号)
- flex九宫格布局
- SQLI-LABS通关(less1)
- Common function writing method and set get writing method for calculating attributes
猜你喜欢

Latex warning: citation "*****" on page y undefined on input line*

20201002 vs 2019 qt5.14 developed program packaging

CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現

Uniapp introduces local fonts

在php的开发环境中如何调取WebService?

查询GPU时无进程运行,但是显存却被占用了

Blog directory of zzq -- updated on 20210601

Flex Jiugongge layout

Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件

Go package name
随机推荐
CTF web practice competition
sqli-labs通关汇总-page2
Improve user experience defensive programming
In depth study of JVM bottom layer (V): class loading mechanism
js中对于返回Promise对象的语句如何try catch
Pytest (1) case collection rules
Wechat applet Foundation
Anti shake and throttling of JS
php中根据数字月份返回月份的英文缩写
Record RDS troubleshooting once -- RDS capacity increases dramatically
PgSQL learning notes
VSCODE 安装LATEX环境,参数配置,常见问题解决
uniapp引入本地字体
The table component specifies the concatenation parallel method
flex九宫格布局
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
Eggjs -typeorm treeenity practice
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
微信小程序基础
js中map和forEach的用法