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

cipsas's avatar
Level 10

Sql query

Hi,

here is sql query:

select
   `attribute_values`.`id`,
   `attribute_values`.`title` as `theme_title`,
   (
      SELECT
         SUM(product_variants.inventory_quantity) 
      FROM
         product_variants 
      WHERE
         product_variants.product_id = attributables.entity_id 
   )
   as stock,
   (
      SELECT
         SUM( 
         CASE
            WHEN
               orders.type = "order" 
            THEN
               order_products.stocked_quantity 
            WHEN
               orders.type = "return" 
            THEN
               - order_products.stocked_quantity 
         END
) 
      FROM
         order_products 
         LEFT JOIN
            orders 
            ON order_products.order_id = orders.id 
      WHERE
         order_products.product_id = attributables.entity_id 
   )
   as sold 
from
   `attribute_values` 
   left join
      `attributables` 
      on `attribute_values`.`id` = `attributables`.`attribute_value_id` 
      and `attributables`.`entity_type` = ? 
where
   `attribute_values`.`attribute_id` = ? 
group by
   `attribute_values`.`id`

But I got an error:

 Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'attributables.entity_id' which is not functionally dependent on columns in GROUP BY clause;

If I add column attributables.entity_id to Group BY. It multiply records. How to solve this case?

0 likes
0 replies

Please or to participate in this conversation.