没有搞明白,求高人解惑啊!
数据表:
- DROP TABLE IF EXISTS president;
- #@ _CREATE_TABLE_
- CREATE TABLE president
- (
- last_name VARCHAR(15) NOT NULL,
- first_name VARCHAR(15) NOT NULL,
- suffix VARCHAR(5) NULL,
- city VARCHAR(20) NOT NULL,
- state VARCHAR(2) NOT NULL,
- birth DATE NOT NULL,
- death DATE NULL
- );
数据:
- INSERT INTO president VALUES ('Roosevelt','Franklin D.',NULL,'Hyde Park','NY','1882-01-30','1945-04-12');
- INSERT INTO president VALUES ('Truman','Harry S',NULL,'Lamar','MO','1884-05-08','1972-12-26');
- INSERT INTO president VALUES ('Eisenhower','Dwight D.',NULL,'Denison','TX','1890-10-14','1969-03-28');
- INSERT INTO president VALUES ('Kennedy','John F.',NULL,'Brookline','MA','1917-05-29','1963-11-22');
- INSERT INTO president VALUES ('Johnson','Lyndon B.',NULL,'Stonewall','TX','1908-08-27','1973-01-22');
- INSERT INTO president VALUES ('Nixon','Richard M.',NULL,'Yorba Linda','CA','1913-01-09','1994-04-22');
- INSERT INTO president VALUES ('Ford','Gerald R.',NULL,'Omaha','NE','1913-07-14','2006-12-26');
- INSERT INTO president VALUES ('Carter','James E.','Jr.','Plains','GA','1924-10-01',NULL);
- INSERT INTO president VALUES ('Reagan','Ronald W.',NULL,'Tampico','IL','1911-02-06','2004-06-05');
- INSERT INTO president VALUES ('Bush','George H.W.',NULL,'Milton','MA','1924-06-12',NULL);
- INSERT INTO president VALUES ('Clinton','William J.',NULL,'Hope','AR','1946-08-19',NULL);
- INSERT INTO president VALUES ('Bush','George W.',NULL,'New Haven','CT','1946-07-06',NULL);
当我们对death做升序排序的时候
- mysql> select last_name, first_name, death from president order by death asc limit 10;
- +------------+-------------+------------+
- | last_name | first_name | death |
- +------------+-------------+------------+
- | Bush | George W. | NULL |
- | Clinton | William J. | NULL |
- | Bush | George H.W. | NULL |
- | Carter | James E. | NULL |
- | Roosevelt | Franklin D. | 1945-04-12 |
- | Kennedy | John F. | 1963-11-22 |
- | Eisenhower | Dwight D. | 1969-03-28 |
- | Truman | Harry S | 1972-12-26 |
- | Johnson | Lyndon B. | 1973-01-22 |
- | Nixon | Richard M. | 1994-04-22 |
- +------------+-------------+------------+
- 10 rows in set (0.00 sec)
NULL字段的排在最前面,然后非NULL字段做升序排列
当我们对death做降序排序的时候
- mysql> select last_name, first_name, death from president order by death desc;+------------+-------------+------------+
- | last_name | first_name | death |
- +------------+-------------+------------+
- | Ford | Gerald R. | 2006-12-26 |
- | Reagan | Ronald W. | 2004-06-05 |
- | Nixon | Richard M. | 1994-04-22 |
- | Johnson | Lyndon B. | 1973-01-22 |
- | Truman | Harry S | 1972-12-26 |
- | Eisenhower | Dwight D. | 1969-03-28 |
- | Kennedy | John F. | 1963-11-22 |
- | Roosevelt | Franklin D. | 1945-04-12 |
- | Carter | James E. | NULL |
- | Bush | George H.W. | NULL |
- | Clinton | William J. | NULL |
- | Bush | George W. | NULL |
- +------------+-------------+------------+
- 12 rows in set (0.00 sec)
NULL字段排在最后面
怎么才能够,在做降序排序的时候,NULL在前面,非NULL值在后面了,很明显death为NULL的是还没有去世的名单,别人给出的sql是:
- mysql> select last_name, first_name, death from president order by if(death is null ,0 ,1) ,death desc;
- +------------+-------------+------------+
- | last_name | first_name | death |
- +------------+-------------+------------+
- | Bush | George W. | NULL |
- | Clinton | William J. | NULL |
- | Bush | George H.W. | NULL |
- | Carter | James E. | NULL |
- | Ford | Gerald R. | 2006-12-26 |
- | Reagan | Ronald W. | 2004-06-05 |
- | Nixon | Richard M. | 1994-04-22 |
- | Johnson | Lyndon B. | 1973-01-22 |
- | Truman | Harry S | 1972-12-26 |
- | Eisenhower | Dwight D. | 1969-03-28 |
- | Kennedy | John F. | 1963-11-22 |
- | Roosevelt | Franklin D. | 1945-04-12 |
- +------------+-------------+------------+
- 12 rows in set (0.00 sec)
不知道怎么理解这个sql,求高人解答!