sql - 在 Laravel 中执行自定义查询
问题描述
我尝试执行这样的查询
@type = 'hobby'
IF @type = 'hobby'
BEGIN
SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id
SELECT h.id, h.manId, h.hobbyName, h.timeSchedule, tp.name, tp.parentName from hobby h
join #tempPeople tp on h.manId = tp.id
drop table #tempPeople
END
ELSE
BEGIN
SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id
SELECT gf.id, gf.manId, gf.name, gf.address, tp.name, tp.parentName from girlFriend gf
join #tempPeople tp on gf.manId = tp.id
END
我的laravel代码是这样的
public static function getManControl($type)
{
$sql = "
declare @type varchar(100)
@type = '".$type."'
IF @type = 'hobby'
BEGIN
SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id
SELECT h.id, h.manId, h.hobbyName, h.timeSchedule, tp.name, tp.parentName from hobby h
join #tempPeople tp on h.manId = tp.id
drop table #tempPeople
END
ELSE
BEGIN
SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id
SELECT gf.id, gf.manId, gf.name, gf.address, tp.name, tp.parentName from girlFriend gf
join #tempPeople tp on gf.manId = tp.id
END
";
$result = DB::select($sql);
return $result;
}
上面代码的结果,得到这样的异常
PDOException: SQLSTATE[IMSSP]: 查询的活动结果不包含任何字段。
如何在 laravel 中运行该查询?我应该用什么?
解决方案
推荐阅读
- vue.js - 作为 axios 帖子的一部分,在表单上提交多个文件
- php - Guzzle Http 400 错误请求
- java - 多对多关联出现在 json 响应中的 _links 中
- angular - 错误:EACCES:权限被拒绝,打开“/home/mukunda/.ionic/helper.log”
- spring-security - 在 vaadin flow 14 中排除来自 vaadin 路由器的 URL 路径
- python - 谷歌翻译突然停止工作我没有以任何方式更改代码
- python - ModuleNotFoundError(相对于 healpy)
- javascript - Angular - 如何仅绑定函数中的特定项目
- php - 未捕获的错误:找不到类“PHP_Token_ELLIPSIS”
- python - 如何使用python增加字典中的值