Skip to content

Instantly share code, notes, and snippets.

@werty1st
Created July 24, 2014 18:10
Show Gist options
  • Save werty1st/ceefffbdf509d05ca4ab to your computer and use it in GitHub Desktop.
Save werty1st/ceefffbdf509d05ca4ab to your computer and use it in GitHub Desktop.
mysql join IF sum
SELECT ci.inventory_id, ci.inventory_quantity, cd.reservations, (ci.inventory_quantity + cd.reservations) as gesamt
FROM catalog_inventory ci
JOIN
(SELECT cd.documentation_inventory_nr, SUM(IF(cd.documentation_on >= UNIX_TIMESTAMP(NOW()),cd.documentation_quantity,0)) as reservations
FROM
catalog_documentation cd
GROUP BY cd.documentation_inventory_nr) as cd
ON cd.documentation_inventory_nr=ci.inventory_id
GROUP BY ci.inventory_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment