duan0504 2017-03-08 00:21
浏览 100

PostgreSQL / Laravel sum和groupBy结果

I need to sum all values in table by links_id

CREATE TABLE links_day
(
    id BIGINT DEFAULT nextval('links_day_id_seq'::regclass) PRIMARY KEY NOT NULL,
    links_id BIGINT NOT NULL,
    users_id BIGINT NOT NULL,
    redirect_js_meta BOOLEAN,
    slow_redirect_checker BOOLEAN,
    cost BIGINT,
    name VARCHAR(255) NOT NULL,
    track_link VARCHAR(255) NOT NULL,
    tcl BIGINT,
    ucl BIGINT,
    fcl BIGINT,
    bcl BIGINT,
    act BIGINT,
    eng BIGINT,
    usl BIGINT,
    rsl BIGINT,
    date DATE NOT NULL,
    created_at TIMESTAMP(0),
    updated_at TIMESTAMP(0)
);

I am using Laravel query builder for this:

$links = DB::table('links_day')
            ->select(
                'id',
                DB::raw('sum(tcl) as tcl'),
                DB::raw('sum(ucl) as ucl'),
                DB::raw('sum(fcl) as fcl'),
                DB::raw('sum(bcl) as bcl'),
                DB::raw('sum(act) as act'),
                DB::raw('sum(eng) as eng'),
                DB::raw('sum(usl) as usl'),
                DB::raw('sum(rsl) as rsl'),
                'links_id',
                'users_id',
                'redirect_js_meta',
                'slow_redirect_checker',
                'cost',
                'name',
                'track_link',
                'created_at'
                )
            ->where('users_id', Auth::user()->id)->groupBy('links_id')

I have the error for this: "id must appear in the GROUP BY clause or be used in an aggregate function". Error disappear when adding all non-sum column, but then I don't back right results, I need sum value by links_id.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大