首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在Laravel中设置db表的列

无法在Laravel中设置db表的列
EN

Stack Overflow用户
提问于 2022-06-07 13:41:14
回答 1查看 38关注 0票数 1

我是新来的拉拉。我的数据库里有三张桌子

带字段的

  1. dsp_account id,name_display,short_name.

带字段的

  1. dsp id,dsp_account_id.

带字段的

  1. rtbcfg_region_dsp dsp_id,status.

dsp表通过dsp_account字段连接到dsp_account_id表。

dsp表通过rtbcfg_region_dsp与dsp_id连接。

我尝试创建这个sql查询:

代码语言:javascript
运行
复制
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;

这是我的密码:

代码语言:javascript
运行
复制
$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();

这就是我所犯的错误:我错过了什么:

代码语言:javascript
运行
复制
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) 

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-07 14:12:36

代码语言:javascript
运行
复制
$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,因为它是模型类名。我还没有调试查询,但我只回答了语法错误。谢谢:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72532263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档