【发布时间】:2018-11-14 16:51:54
【问题描述】:
所以这个查询是在用户|sticky bit| 的帮助下完成的。效果很好,但现在我不知道如何将它翻译成 Laravel Eloquent 结构。
UPDATE knowns
SET engagementtitle = (SELECT instructions.engagementtitle
FROM instructions
WHERE instructions.reference = knowns.reference)
WHERE EXISTS (SELECT *
FROM instructions
WHERE instructions.reference = knowns.reference);
我正在努力解决的部分是 Eloquent 语句的组装顺序不明显。
我尝试过让内部部件正常工作,然后尝试进一步包裹它们的方法。所以 WHERE EXISTS 子句可以这样创建:
DB::table('knowns')
->whereExists(function ($query) {
$query->select(DB::raw('1'))
->from('instructions')
->whereRaw('instructions.reference = knowns.reference');
})
->get();
哪个产生
select * from "knowns"
where exists (select 1 from "instructions" where instructions.reference = knowns.reference)"
但我没有进一步包装这部分。一方面,我无法在文档中找到关键字“SET”的等效项。
这是我生成上述 SQL 语句的完整尝试。
DB::table('knowns')
->update(['instructions.reference' => 'knowns.reference'])
->select('instructions.engagementtitle')
->from('instructions')
->where('instructions.reference=knowns.reference')
->whereExists(function ($query) {
$query->select(DB::raw('1'))
->from('instructions')
->whereRaw('instructions.reference = knowns.reference');
})
->get();
(为狗的早餐道歉 - 它甚至 看起来 很糟糕)
编辑: 或者我可以只使用这样的 PDO 方法....
$PDO=DB::connection()->getPdo();
$stmt=$PDO->prepare("
...SQL STATEMENT...
");
$stmt->execute();
$result = $stmt->fetchAll();
但我仍然想知道是否有 Eloquent 方法。
【问题讨论】:
-
先准备关系和模型,然后尝试用eloquent操作
-
我考虑过关系......但在这种情况下,我认为这不是一个好主意。问题是指令可以随时刷新,我不确定是否要与非静态数据集建立关系。?