我是新来的拉拉。我的数据库里有三张桌子
带字段的
dsp_account id,name_display,short_name.

带字段的
dsp id,dsp_account_id.

。
带字段的
rtbcfg_region_dsp dsp_id,status.

。
dsp表通过dsp_account字段连接到dsp_account_id表。
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)

发布于 2022-06-07 14:12:36
$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://stackoverflow.com/questions/72532263
复制相似问题