当前位置:网站首页>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']);//这里写入数据表的字段值
边栏推荐
- ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
- Implement strstr() II
- Linux MySQL 5.6.51 community generic installation tutorial
- unittest. Texttestrunner does not generate TXT test reports
- CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
- Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
- Underlying mechanism mvcc
- Improve user experience defensive programming
- SQLI-LABS通关(less1)
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
猜你喜欢
随机推荐
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
Atcoder beginer contest 253 F - operations on a matrix / / tree array
js删除字符串的最后一个字符
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Linux MySQL 5.6.51 community generic installation tutorial
Common function writing method and set get writing method for calculating attributes
JS create a custom JSON array
PXC high availability cluster summary
SQLI-LABS通關(less6-less14)
查询GPU时无进程运行,但是显存却被占用了
蚂蚁集团g6初探
浏览器滚动加载更多实现
CTF web practice competition
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
Sqli-labs customs clearance (less15-less17)
Wechat applet Foundation
Queue (linear structure)
Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
Sqli - Labs Clearance (less6 - less14)
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現









