Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created September 16, 2011 13:23
Show Gist options
  • Select an option

  • Save tobiashm/1222122 to your computer and use it in GitHub Desktop.

Select an option

Save tobiashm/1222122 to your computer and use it in GitHub Desktop.
SQL snippets
-- initial
UPDATE item SET depth = 0 WHERE parent_item_guid IS NULL;
-- SQLite3 doesn't support loops, so run this until `select count(*) from item where product_code is null` = 0
INSERT OR REPLACE INTO item(item_guid, user_guid, parent_item_guid, name, depth)
SELECT t.item_guid, t.user_guid, t.parent_item_guid, t.name, p.depth + 1 FROM item AS t
INNER JOIN item AS p ON (t.parent_item_guid = p.item_guid)
WHERE p.depth >= 0
AND NOT p.item_guid IS NULL
AND t.depth IS NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment