M
M
Mikhail Smirnov2021-12-23 22:10:48
Laravel
Mikhail Smirnov, 2021-12-23 22:10:48

Why does it give an error when querying the database?

$items = DB::table('catalog_index AS f')
            ->select(DB::raw("
                cp.name,
                cp.type,
                f.facet_id,
                f.value,
                MIN(f.value_num) min_value_num,
                MAX(f.value_num) max_value_num,
                COUNT(DISTINCT f.product_id) AS cnt
            "))
            ->join('catalog_properties AS cp', 'cp.id', '=', 'f.facet_id')
            ->where('f.section_id', '=', 440)
            ->groupBy(['f.facet_id', 'f.value'])
            ->get();


Gives an error message:

SQLSTATE[42000]: Syntax error or access violation: 1055 'test-db.cp.name' isn't in GROUP BY (SQL: select cp.name, cp.type, f.facet_id, f.value, MIN(f .value_num) min_value_num, MAX(f.value_num) max_value_num, COUNT(DISTINCT f.product_id) AS cnt from `catalog_index` as `f` inner join `catalog_properties` as `cp` on `cp`.`id` = `f `.`facet_id` where `f`.`section_id` = 440 group by `f`.`facet_id`, `f`.`value`)


If you copy the resulting SQL query to the console, it works without problems.

also if from select to remove both fields "cp." then the error in laravel disappears

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2021-12-23
@fortoster82

strict connection setting if memory doesn't lie

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question