Here is my MySQL Query:
$query = "SELECT
ws_inventory.id,
ws_inventory.SKU AS inventory_sku,
ws_inventory.Quantity AS inventory_quantity,
ws_mastersku.SKU_1,
ws_mastersku.SKU_2,
ws_mastersku.SKU_4,
ws_mastersku.SKU_3,
ws_mastersku.MultSKU,
ws_mastersku.QtySKU,
ws_mastersku.AltSKU,
ws_mastersku.SKU,
ws_sold.SKU AS sold_sku,
ws_sold.Quantity AS sold_quantity
FROM ws_sold
LEFT OUTER JOIN ws_mastersku
ON ws_sold.sku = ws_mastersku.SKU
LEFT OUTER JOIN ws_inventory
ON ws_mastersku.SKU_1 = ws_inventory.SKU
OR ws_mastersku.AltSKU = ws_inventory.SKU
GROUP BY ws_sold.sku;";
But it's only returning the first instance of WS16
Even though my table has two entries for WS16
How can I modify my Query so that it will select ALL the rows? (in my last picture it is omitting row id #4)
The new select query should look like (which as you can see the fourth column here)
1 WS16 91 (null) (null) (null) (null) 0 4 WS16 WS16X4-2 WS16X4-2 2
2 WS3 97 (null) (null) (null) (null) 0 2 WS3 WS3X2-4 WS3X2-4 1
3 WS6 95 (null) (null) (null) (null) 0 4 WS6 WS6X4-16 WS6X4-16 1
4 WS16 75 (null) (null) (null) (null) 0 4 WS16 WS16X4-2 WS16X4-2 4
Per Keith and Tim's kind responses, I've tried the query without the GROUP BY
clause and here is my result
It is a step in the right direction but the inventory_quantity
is wrong..
This is because in my PHP script I am doing
$newQuantity = $rs['inventory_quantity'] - ($rs['sold_quantity'] * $rs['QtySKU']);
And the output of the script in my while loop is
UPDATE ws_inventory SET Quantity = 91 WHERE SKU = 'WS16'
followed by
UPDATE ws_inventory SET Quantity = 83 WHERE SKU = 'WS16'
Issue is because inventory_quantity
is taken at the start the query.
Quantity for first statement is -8 (start 99), and second statement is -16 (start 99). Need to know figure out a way to get it so that statements will work with eachother in this case, and do a 99 - 8 - 16 = 75
instead of just overwriting the result..