当前位置:网站首页>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']);//这里写入数据表的字段值
边栏推荐
- Browser scrolling for more implementations
- 浏览器滚动加载更多实现
- Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
- Latex compiles Chinese in vscode and solves the problem of using Chinese path
- Huawei mindspire open source internship machine test questions
- Solve the problem of bindchange event jitter of swiper component of wechat applet
- No process runs when querying GPU, but the video memory is occupied
- js中对于返回Promise对象的语句如何try catch
- Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
- table 组件指定列合并行方法
猜你喜欢

Redis -- cache breakdown, penetration, avalanche

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

Usage of map and foreach in JS

PHP Session原理简析

unittest. Texttestrunner does not generate TXT test reports

Latex compilation error I found no \bibstyle &\bibdata &\citation command

apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted

js中对于返回Promise对象的语句如何try catch

Linux MySQL 5.6.51 community generic installation tutorial

Flex Jiugongge layout
随机推荐
Common prototype methods of JS array
Vscode installation, latex environment, parameter configuration, common problem solving
Stress test modification solution
Flask migrate cannot detect db String() equal length change
Latex compilation error I found no \bibstyle &\bibdata &\citation command
Promise中有resolve和无resolve的代码执行顺序
Flex Jiugongge layout
js中map和forEach的用法
js删除字符串的最后一个字符
Differences between ts and JS
Atcoder beginer contest 253 F - operations on a matrix / / tree array
js删除字符串的最后一位
Sublime text configuring PHP compilation environment
2021-07-05C#/CAD二次开发创建圆弧(4)
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
Win电脑截图黑屏解决办法
20210306 reprint how to make TextEdit have background pictures
默认google浏览器打不开链接(点击超链接没有反应)
SQLI-LABS通关(less2-less5)
MySQL index