A
Anonymous
Guest
'm trying to share the quantity of products of the same group (if not zero), using a query.
It's a website using PHP5.6 and MySQL queries.
Database table Products
+-------------------+-----------------------------+------------------------------+
| products_id | products_quantity | products_master |
+-------------------+-----------------------------+------------------------------+
|--------11--------|---------------1--------------|--------------12-------------|
|--------12--------|---------------5--------------|--------------12-------------|
|--------13--------|---------------2--------------|--------------12-------------|
|--------14--------|---------------7--------------|--------------0---------------|
|--------15--------|---------------9--------------|--------------0---------------|
+------------------+------------------------------+------------------------------+
Expected results
+-------------------+-----------------------------+------------------------------+
| products_id | products_quantity | products_master |
+-------------------+-----------------------------+------------------------------+
|--------11--------|---------------8--------------|--------------12-------------|
|--------12--------|---------------8--------------|--------------12-------------|
|--------13--------|---------------8--------------|--------------12-------------|
|--------14--------|---------------7--------------|--------------0---------------|
|--------15--------|---------------9--------------|--------------0---------------|
+------------------+------------------------------+------------------------------+
I need help write this code
Products with the same products_master (if not zero) have the sum of quantity.
Someone provided a solution but the query is not returning any rows :
It's a website using PHP5.6 and MySQL queries.
Database table Products
+-------------------+-----------------------------+------------------------------+
| products_id | products_quantity | products_master |
+-------------------+-----------------------------+------------------------------+
|--------11--------|---------------1--------------|--------------12-------------|
|--------12--------|---------------5--------------|--------------12-------------|
|--------13--------|---------------2--------------|--------------12-------------|
|--------14--------|---------------7--------------|--------------0---------------|
|--------15--------|---------------9--------------|--------------0---------------|
+------------------+------------------------------+------------------------------+
Expected results
+-------------------+-----------------------------+------------------------------+
| products_id | products_quantity | products_master |
+-------------------+-----------------------------+------------------------------+
|--------11--------|---------------8--------------|--------------12-------------|
|--------12--------|---------------8--------------|--------------12-------------|
|--------13--------|---------------8--------------|--------------12-------------|
|--------14--------|---------------7--------------|--------------0---------------|
|--------15--------|---------------9--------------|--------------0---------------|
+------------------+------------------------------+------------------------------+
I need help write this code
Code:
$sql = "SELECT products_id, IF(products_master != 0, (missing code to sum quantity), products_quantity) AS products_quantity FROM products ORDER BY products_quantity";
Products with the same products_master (if not zero) have the sum of quantity.
Someone provided a solution but the query is not returning any rows :
Code:
SELECT p.products_id, CASE products_master WHEN 0 THEN products_quantity ELSE x.qty END
from products p
left join (SELECT products_master, SUM (products_quantity) AS qty FROM Products GROUP BY products_master) x on p.products_master = x.products_master