Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

virtual's avatar

fetch all data two table with count item individually groupby

Hi i am get a sql error, the error is

Syntax error or access violation: 1055 'sitemap.products.id' isn't in GROUP BY (SQL: select products.*, COUNT(products.brand_id) as total from products left join brand on products.brand_id = brand.brand_id group by products.brand_id)

One table is Brand CREATE TABLE brand ( brand_id int(11) NOT NULL, name varchar(255) NOT NULL ) Another table Products CREATE TABLE products ( id int(11) NOT NULL, name varchar(255) NOT NULL, slug varchar(255) NOT NULL, text longtext NOT NULL, new_price int(11) NOT NULL, old_price int(11) NOT NULL, image varchar(255) NOT NULL, video text NOT NULL, cat_id int(11) NOT NULL, brand_id int(11) NOT NULL, boolean_used int(11) NOT NULL, boolean_highlighted int(11) NOT NULL, deleted_at datetime NOT NULL, updated_at datetime NOT NULL, created_at datetime NOT NULL, published_at datetime NOT NULL )

And my sql query

$products=DB::table('products')->select('products.*', \DB::raw('COUNT(products.brand_id) as total'))->leftJoin('brand','products.brand_id','=','brand.brand_id')->groupBy('products.brand_id')->get();

her i need all data of products table and brand table brand name. at the same time i need to count product.brand_id groupby. how can i fixed it. please help me. thanks in advanced.

0 likes
0 replies

Please or to participate in this conversation.