当前位置:网站首页>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']);//这里写入数据表的字段值
边栏推荐
- Brief analysis of PHP session principle
- JS delete the last bit of the string
- Utilisation de la carte et de foreach dans JS
- SQLI-LABS通關(less6-less14)
- MySQL index
- In depth study of JVM bottom layer (V): class loading mechanism
- Usage of map and foreach in JS
- Sqli-labs customs clearance (less15-less17)
- Sentry construction and use
- ZZQ的博客目录--更新于20210601
猜你喜欢
SQLI-LABS通关(less1)
In depth study of JVM bottom layer (IV): class file structure
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Sqli - Labs Clearance (less6 - less14)
Sqli labs customs clearance summary-page3
Apt command reports certificate error certificate verification failed: the certificate is not trusted
蚂蚁集团g6初探
Linux MySQL 5.6.51 community generic installation tutorial
Huawei mindspire open source internship machine test questions
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
随机推荐
Eslint configuration code auto format
The default Google browser cannot open the link (clicking the hyperlink does not respond)
MySQL index
sqli-labs通关汇总-page2
Sqli-labs customs clearance (less6-less14)
MySQL中的正则表达式
Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
Sqli - Labs Clearance (less6 - less14)
sqli-labs通关汇总-page1
Build learning tensorflow
PIP install
Utilisation de la carte et de foreach dans JS
Uniapp introduces local fonts
UEditor .Net版本任意文件上传漏洞复现
Redis -- cache breakdown, penetration, avalanche
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
SQLI-LABS通关(less2-less5)
20201025 visual studio2019 qt5.14 use of signal and slot functions
ts和js区别
Flask migrate cannot detect db String() equal length change