我是 laravel 的新手。我的資料庫中有三個表
dsp_account與欄位id,name_display,short_name。

dsp與欄位id,dsp_account_id。
.
rtbcfg_region_dsp與欄位dsp_id,status。
.
dsp 表通過dsp_account_id欄位連接到 dsp_account 表。
dsp 表通過 連接到 rtbcfg_region_dsp dsp_id。
我嘗試創建這個 sql 查詢:
select distinct dsp_account.id,dsp_account.name_display,dsp_account.short_name
from dsp_account
inner join dsp on dsp.dsp_account_id = dsp_account.id
AND dsp.id
IN(SELECT dsp_id
FROM rtbcfg_region_dsp
group by dsp_id, status
having count(*)>0 and status = 1) order by dsp_account.name_display ASC;
這是我的代碼:
$all_list6 = DspAccount::select('DspAccount.id', 'DspAccount.name_display','DspAccount.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dspAccount.id')
->whereIn('DSP.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->get();
這是我得到的錯誤:我錯過了什么:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DspAccount.id' in 'field list' (SQL: select `DspAccount`.`id`, `DspAccount`.`name_display`, `DspAccount`.`short_name` from `dsp_account` inner join `dsp` on `dsp`.`dsp_account_id` = `dspAccount`.`id` where `DSP`.`id` in (select `dsp_id` from `rtbcfg_region_dsp` group by `dsp_id`, `status` having COUNT(*) > 0 AND status = 1) and `dsp_account`.`deleted_at` is null)

uj5u.com熱心網友回復:
$all_list6 = DspAccount::select('dsp_account.id', 'dsp_account.name_display','dsp_account.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dsp_account.id')
->whereIn('dsp.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->distinct()->get();
您應該使用原始表名dsp_account而不是DspAccount模型類名。我沒有除錯查詢,但我只回答了語法錯誤。謝謝:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490785.html
