Mysql – Cannot retrieve values from LEFT JOIN (2 tables)

MySQL

Here it is my query:

$query_products = "
SELECT 
    p.Id
    , p.product_sku
    , p.product_name
    , p.hide_product
    , p.new_style_image
    , p.ext
    , p.product_thumb_image
    , p.product_full_image
    , SUM(s.stoc_final)
    , s.active 
FROM 
    produse p 
    LEFT JOIN stoc_intern s ON p.product_sku=s.cod_website 
WHERE 
    (
        (" . $product_sku_search . ") 
        OR (" . $product_name_search . ") 
        OR ("  .$product_culoare_search . ")
    ) 
    AND p.hide_product != 'd' 
GROUP by p.product_sku 
ORDER BY SUM(s.stoc_final) DESC 
LIMIT " . $items_per_page . " 
OFFSET " . $_offset . "";

My problem is: the last field I'm selecting, the s.active one, has in the table the value of "n" but when I display it, it shows empty (echo $s_active);
If I change the field s.active with say other field s.description, it will work.

Also, in the table "stoc_intern" some fields that I select do display well, some do not display a thing (even if table data contains info).

Why is that?

Best Answer

a_horse_with_no_name already provided with the answer.

But to disable this "one of those stupid MySQL "features" that no one can really understand", you can set sql_mode to be more strict about it. Consider the following setup in your my.cnf config file:

[mysqld]
sql_mode = ONLY_FULL_GROUP_BY

See documentation. This will not allow you to reference non-grouped columns unless with an aggregation function.