Skip to content

Instantly share code, notes, and snippets.

@aohorodnyk
Created September 23, 2014 14:00
Show Gist options
  • Save aohorodnyk/4b556f1add61ae568526 to your computer and use it in GitHub Desktop.
Save aohorodnyk/4b556f1add61ae568526 to your computer and use it in GitHub Desktop.
drop TABLE test.t1;
create table `test`.`t1` (`id` INT AUTO_INCREMENT PRIMARY KEY, `attribute_id` INT, `store` INT, `value` INT);
INSERT INTO `test`.`t1` VALUES (NULL, 1, 0, 1), (NULL, 1, 2, 2), (NULL, 2, 0, 3), (NULL, 2, 1, 4), (NULL, 3, 0, 4);
SELECT IFNULL(current_store.value, deafult_store.value) AS value, COUNT(*)
FROM test.t1 AS deafult_store
LEFT JOIN test.t1 AS current_store
ON (deafult_store.attribute_id = current_store.attribute_id AND current_store.store = 1)
WHERE deafult_store.store = 0
GROUP BY `value`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment