Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created January 15, 2013 04:09
Show Gist options
  • Save grodtron/4535967 to your computer and use it in GitHub Desktop.
Save grodtron/4535967 to your computer and use it in GitHub Desktop.
MySQL Trick to fairly quickly generate a range of numbers. I thought it was cool :)
DROP DATABASE IF EXISTS range_test;
CREATE DATABASE range_test;
USE range_test;
CREATE TABLE bools (
v BOOLEAN
);
INSERT INTO bools (v) VALUES (0), (1);
CREATE TABLE ints (
v INT
);
-- Using bitwise shifts and ORs seems to make a difference vs. multiplications and additions
INSERT INTO ints (v)
SELECT (a.v<<0 | b.v<<1 | c.v<<2 | d.v<<3 | e.v<<4 | f.v<<5 | g.v<<6 | h.v<<7 | i.v<<8) FROM
bools a CROSS JOIN
bools b CROSS JOIN
bools c CROSS JOIN
bools d CROSS JOIN
bools e CROSS JOIN
bools f CROSS JOIN
bools g CROSS JOIN
bools h CROSS JOIN
bools i;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment