gpt4 book ai didi

php - 使用 select 选择不在的地方

转载 作者:行者123 更新时间:2023-12-04 15:24:10 26 4
gpt4 key购买 nike

我正在尝试在 SQL SERVER 中执行此操作


SELECT cast(datediff(DAY, min([fbh].FBH_DATA_INICIAL), CASE
WHEN max([fbh].FBH_DATA_Final) = '9999-12-31' THEN cast(getdate() AS date)
ELSE max([fbh].FBH_DATA_Final)
END)AS FLOAT) / CAST(365 AS FLOAT) AS duration
FROM [funcionario] AS [f]
INNER JOIN [funcionario_banda_historico] AS [fbh] ON [f].[FUN_ID] = [fbh].[FUN_ID]
AND [f].[FUN_BANDA] = [fbh].[FUN_BANDA]
WHERE NOT EXISTS
(SELECT 1
FROM funcionario_banda_historico t1
WHERE t1.fun_id = [fbh].fun_id
AND t1.FBH_DATA_INICIAL > [fbh].FBH_DATA_Final
AND t1.FUN_BANDA <> [fbh].FUN_BANDA )
AND [f].[FUN_ID] = '9999999'
GROUP BY f.fun_id,
[f].[FUN_BANDA]

我试图在不在 LARAVEL 中的地方进行选择

protected function getYearsInBand($userId) {
$fbh = DB::table('funcionario as f')
->join('funcionario_banda_historico as fbh', function($join) {
$join->on('f.FUN_ID', '=', 'fbh.FUN_ID');
$join->on('f.FUN_BANDA', '=', 'fbh.FUN_BANDA');
})
->selectRaw('cast(datediff(day,min([fbh].FBH_DATA_INICIAL),case when max([fbh].FBH_DATA_Final) = "9999-12-31" then cast(getdate() as date) else max([fbh].FBH_DATA_Final) end)AS FLOAT)/CAST(365 AS FLOAT) AS duration')
->where('f.FUN_ID', $userId)
->whereNotIn('1', function($q) {
$q->select('*')
->from('funcionario_banda_historico as t1')
->where('t1.fun_id = [fbh].fun_id')
->where('t1.FBH_DATA_INICIAL > [fbh].FBH_DATA_Final')
->where('t1.FUN_BANDA <> [fbh].FUN_BANDA')
->get();
})
->groupBy('f.FUN_ID', 'f.FUN_BANDA')
->first();

if (!$fbh) {
\Log::debug('funcionario_banda_historico not found'); // Grava no log
return 0;
}

return $fbh->duration;
}

我遇到了错误 Invalid column name 'fun_id = [fbh]'

你能帮帮我吗?

最佳答案

我认为 Laravel 中“where”子句的语法不正确。相反,你应该这样做:

->whereRaw('t1.FUN_ID = fbh.FUN_ID')
->whereRaw('t1.FBH_DATA_INICIAL > fbh.FBH_DATA_Final')
->whereRaw('t1.FUN_BANDA <> fbh.FUN_BANDA')

或者:

->where('t1.FUN_ID', 'fbh.FUN_ID')
->where('t1.FBH_DATA_INICIAL', '>', 'fbh.FBH_DATA_Final')
->where('t1.FUN_BANDA', '<>', 'fbh.FUN_BANDA')

请注意,我也将“fun_id”更改为大写,就像您代码中的所有地方一样。我不知道“[fbh]”在 Laravel 中是否正确,尝试只输入“fbh”。对于原始示例,您可能还需要以这种方式添加数据库前缀 - “DB::getTablePrefix()”

关于php - 使用 select 选择不在的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62662367/

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