Skip to content

Instantly share code, notes, and snippets.

@krusynth
Created May 12, 2015 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krusynth/dc17a582fe3e73a16f7d to your computer and use it in GitHub Desktop.
Save krusynth/dc17a582fe3e73a16f7d to your computer and use it in GitHub Desktop.
Handling multifield dates. Please don't do this. It's bad - unmanageable, unreadable.
mysql> desc testdates;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| year | smallint(5) unsigned | YES | | NULL | |
| month | tinyint(3) unsigned | YES | | NULL | |
| day | tinyint(3) unsigned | YES | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
mysql> select * FROM testdates;
+----+------+-------+------+
| id | year | month | day |
+----+------+-------+------+
| 1 | 2004 | 0 | 0 |
| 2 | 2003 | 6 | 5 |
| 3 | 2008 | 3 | 21 |
| 4 | 2005 | 11 | 0 |
+----+------+-------+------+
mysql> SELECT * FROM testdates WHERE (year=2004 AND month>7) OR (year>2004 AND year<2006) OR (year=2006 AND month<6);
+----+------+-------+------+
| id | year | month | day |
+----+------+-------+------+
| 4 | 2005 | 11 | 0 |
+----+------+-------+------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment