gpt4 book ai didi

php - Laravel 等效查询字段 +1 插入数据

转载 作者:行者123 更新时间:2023-11-30 21:47:49 26 4
gpt4 key购买 nike

我想使用 laravel 查询生成器执行 sql 查询。我的 MySQL 查询如下:

insert into $tbl (code,name) values ('code+1','$name');

我可以通过使用以下查询来做到这一点

$id = DB::table($tbl)->max('code');
$data = ['code' => $id +1,'name' => $name];
$query = DB::table($tbl)->insert($data);

但我只想运行单个查询,但不想运行两个查询来优化性能。

这条sql单条查询是什么?

最佳答案

试试这个查询.. 我在这里使用子查询,您可以编辑和使用它

insert into $tbl values (((select max(code) from $tbl as code) + 1), '$name')

关于php - Laravel 等效查询字段 +1 插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48616992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com