To find the row with the most recent birth date, the query is similar, except that you sort in descending orde : Date « Data Type « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Data Type » Date 




To find the row with the most recent birth date, the query is similar, except that you sort in descending orde
    
r
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  id              INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->  name    CHAR(20NOT NULL,
    ->  birth   DATE,
    ->  color   ENUM('blue','red','green','brown','black','white'),
    ->  foods   SET('lutefisk','burrito','curry','eggroll','fadge','pizza'),
    ->  cats    INT,
    ->  PRIMARY KEY (id)
    -> );
Query OK, rows affected (0.00 sec)

mysql> INSERT INTO mytable
    ->  VALUES
    ->          (NULL,'Jack','1970-04-13','black','eggroll,pizza,fadge',0),
    ->          (NULL,'Tom','1969-09-30','white','curry,eggroll,burrito',3),
    ->          (NULL,'Mary','1957-12-01','red','burrito,pizza,curry',1),
    ->          (NULL,'Jane','1973-11-02','red','pizza,eggroll',4),
    ->          (NULL,'Sean','1963-07-04','blue','burrito,curry',5),
    ->          (NULL,'Alan','1965-02-14','red',',curry,eggroll',1),
    ->          (NULL,'March','1968-09-17','green','fadge,lutefisk',1),
    ->          (NULL,'Shane','1975-09-02','black','pizza,curry',2),
    ->          (NULL,'Dan','1952-08-20','green','fadge,lutefisk',0),
    ->          (NULL,'Tony','1960-05-01','white','pizza,burrito',0)
    -> ;
Query OK, 10 rows affected, warning (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 1

mysql>
mysql> SELECT FROM mytable;
+----+-------+------------+-------+-----------------------+------+
| id | name  | birth      | color | foods                 | cats |
+----+-------+------------+-------+-----------------------+------+
|  | Jack  | 1970-04-13 | black | eggroll,fadge,pizza   |    |
|  | Tom   | 1969-09-30 | white | burrito,curry,eggroll |    |
|  | Mary  | 1957-12-01 | red   | burrito,curry,pizza   |    |
|  | Jane  | 1973-11-02 | red   | eggroll,pizza         |    |
|  | Sean  | 1963-07-04 | blue  | burrito,curry         |    |
|  | Alan  | 1965-02-14 | red   | curry,eggroll         |    |
|  | March | 1968-09-17 | green | lutefisk,fadge        |    |
|  | Shane | 1975-09-02 | black | curry,pizza           |    |
|  | Dan   | 1952-08-20 | green | lutefisk,fadge        |    |
10 | Tony  | 1960-05-01 | white | burrito,pizza         |    |
+----+-------+------------+-------+-----------------------+------+
10 rows in set (0.00 sec)

mysql>
mysql> SELECT FROM mytable ORDER BY birth DESC LIMIT 1;
+----+-------+------------+-------+-------------+------+
| id | name  | birth      | color | foods       | cats |
+----+-------+------------+-------+-------------+------+
|  | Shane | 1975-09-02 | black | curry,pizza |    |
+----+-------+------------+-------+-------------+------+
row in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, rows affected (0.00 sec)

   
    
    
    
  














Related examples in the same category
1.Date type value in the form '2003-12-31', range 1000-01-01 to 9999-12-31 (3 bytes)
2.Compare with Date value
3.How MySQL deals with incorrect date value
4.Date literal in where clause
5.Date value literal
6.Date type value inside in operator and subquery
7.Date calculation with variable
8.Date default value
9.Sorting by Calendar Day
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.