Thursday, June 7, 2012

Mysql [ Date ] Insert/Select


mysql> create table usr( id int(10) primary key auto_increment, nome longtext, data date);
Query OK, 0 rows affected (0.10 sec)


mysql> insert into usr (nome,data) values('andre','1990-04-07');
Query OK, 1 row affected (0.00 sec)

mysql> insert into usr (nome,data) values('marcelo','1994-03-31');
Query OK, 1 row affected (0.00 sec)

mysql> insert into usr (nome,data) values('baby',CURDATE());
Query OK, 1 row affected (0.00 sec)

mysql> select * from usr;
+----+---------+------------+
| id | nome    | data       |
+----+---------+------------+
|  1 | andre   | 1990-04-07 |
|  2 | marcelo | 1994-03-31 |
|  3 | baby    | 2012-06-07 |
+----+---------+------------+
3 rows in set (0.00 sec)

mysql> select * from usr where month(data) = 4;
+----+-------+------------+
| id | nome  | data       |
+----+-------+------------+
|  1 | andre | 1990-04-07 |
+----+-------+------------+
1 row in set (0.00 sec)

mysql> select * from usr where month(data) > 3;
+----+-------+------------+
| id | nome  | data       |
+----+-------+------------+
|  1 | andre | 1990-04-07 |
|  3 | baby  | 2012-06-07 |
+----+-------+------------+
2 rows in set (0.00 sec)

mysql> select * from usr where month(data) = 3 or month(data) = 6;
+----+---------+------------+
| id | nome    | data       |
+----+---------+------------+
|  2 | marcelo | 1994-03-31 |
|  3 | baby    | 2012-06-07 |
+----+---------+------------+
2 rows in set (0.00 sec)

mysql> select * from pessoa where month(data) = 4 AND year(data) = 1990;
+----+-------+------------+
| id | nome  | data       |
+----+-------+------------+
|  2 | andre | 1990-04-07 |
+----+-------+------------+
1 row in set (0.00 sec)

mysql> insert into usr (nome,data) values('people','1995-09-20');
Query OK, 1 row affected (0.00 sec)

mysql> select * from usr;
+----+---------+------------+
| id | nome    | data       |
+----+---------+------------+
|  1 | andre   | 1990-04-07 |
|  2 | marcelo | 1994-03-31 |
|  3 | baby    | 2012-06-07 |
|  4 | people  | 1995-09-20 |
+----+---------+------------+
4 rows in set (0.00 sec)

mysql> select * from pessoa where month(data) = 4 AND year(data) = 1990;
+----+-------+------------+
| id | nome  | data       |
+----+-------+------------+
|  2 | andre | 1990-04-07 |
+----+-------+------------+
1 row in set (0.00 sec)

mysql> select * from usr where year(data) > 1990 AND year(data) < 2012;
+----+---------+------------+
| id | nome    | data       |
+----+---------+------------+
|  2 | marcelo | 1994-03-31 |
|  4 | people  | 1995-09-20 |
+----+---------+------------+
2 rows in set (0.00 sec)

Its share

0 comentários:

Post a Comment