gpt4 book ai didi

php - 在 Laravel 中创建一个 Insert...Select 语句

转载 作者:IT老高 更新时间:2023-10-28 23:56:07 26 4
gpt4 key购买 nike

我需要为 Laravel 转换此查询,但在尝试使用 Laravel 的 Eloquont ORM 或查询创建 Insert...Select 语句时遇到问题。我不确定我将如何创建此查询。

Insert into Demand (Login, Name, ApptTime, Phone, Physician, Location, FormatName, FormatDate, FormatTime, ApptDate, FormatPhone, cellphone)
Select Login, Name, ApptTime, Phone, Physician, Location, FormatName, FormatDate, FormatTime, ApptDate, FormatPhone, cellphone from " . [dbname] . "
Where " . $where_statement

如何使用 Laravel 的 ORM 创建此查询?

编辑:我不确定这是否清楚,但我在考虑 1 个查询,就像他们在这里做的那样 http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

最佳答案

没有办法在一个查询中执行此操作(除非你使用的是 Laravel 5.7),但是我遇到了同样的问题并且想确保我可以继续使用我用 QueryBuilder 构建的某个选择。

那么你可以做些什么,让事情保持一半的干净并重用之前构建了 select 语句的功能,是这样的:

/**
* Wherever your Select may come from
**/
$select = User::where(...)
->where(...)
->whereIn(...)
->select(array('email','moneyOwing'));
/**
* get the binding parameters
**/
$bindings = $select->getBindings();
/**
* now go down to the "Network Layer"
* and do a hard coded select
*/
$insertQuery = 'INSERT into user_debt_collection (email,dinero) '
. $select->toSql();

\DB::insert($insertQuery, $bindings);

更新 Laravel 5.7

从 Laravel 5.7.17 开始,您可以使用 ->insertUsing()。见 here了解详情。感谢@Soulriser 指出这一点。

所以上面的查询看起来像这样:

DB::table('user_debt_collection')->insertUsing(['email','dinero'], $select);

关于php - 在 Laravel 中创建一个 Insert...Select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533608/

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