没有搞明白,求高人解惑啊!

数据表:

  1. DROP TABLE IF EXISTS president; 
  2. #@ _CREATE_TABLE_ 
  3. CREATE TABLE president 
  4.   last_name  VARCHAR(15) NOT NULL
  5.   first_name VARCHAR(15) NOT NULL
  6.   suffix     VARCHAR(5) NULL
  7.   city       VARCHAR(20) NOT NULL
  8.   state      VARCHAR(2) NOT NULL
  9.   birth      DATE NOT NULL
  10.   death      DATE NULL 
  11. ); 

数据:

  1. INSERT INTO president VALUES ('Roosevelt','Franklin D.',NULL,'Hyde Park','NY','1882-01-30','1945-04-12'); 
  2. INSERT INTO president VALUES ('Truman','Harry S',NULL,'Lamar','MO','1884-05-08','1972-12-26'); 
  3. INSERT INTO president VALUES ('Eisenhower','Dwight D.',NULL,'Denison','TX','1890-10-14','1969-03-28'); 
  4. INSERT INTO president VALUES ('Kennedy','John F.',NULL,'Brookline','MA','1917-05-29','1963-11-22'); 
  5. INSERT INTO president VALUES ('Johnson','Lyndon B.',NULL,'Stonewall','TX','1908-08-27','1973-01-22'); 
  6. INSERT INTO president VALUES ('Nixon','Richard M.',NULL,'Yorba Linda','CA','1913-01-09','1994-04-22'); 
  7. INSERT INTO president VALUES ('Ford','Gerald R.',NULL,'Omaha','NE','1913-07-14','2006-12-26'); 
  8. INSERT INTO president VALUES ('Carter','James E.','Jr.','Plains','GA','1924-10-01',NULL); 
  9. INSERT INTO president VALUES ('Reagan','Ronald W.',NULL,'Tampico','IL','1911-02-06','2004-06-05'); 
  10. INSERT INTO president VALUES ('Bush','George H.W.',NULL,'Milton','MA','1924-06-12',NULL); 
  11. INSERT INTO president VALUES ('Clinton','William J.',NULL,'Hope','AR','1946-08-19',NULL); 
  12. INSERT INTO president VALUES ('Bush','George W.',NULL,'New Haven','CT','1946-07-06',NULL); 

 

当我们对death做升序排序的时候

  1. mysql> select last_name, first_name, death from president order by  death asc limit 10; 
  2. +------------+-------------+------------+ 
  3. | last_name  | first_name  | death      | 
  4. +------------+-------------+------------+ 
  5. | Bush       | George W.   | NULL       | 
  6. | Clinton    | William J.  | NULL       | 
  7. | Bush       | George H.W. | NULL       | 
  8. | Carter     | James E.    | NULL       | 
  9. | Roosevelt  | Franklin D. | 1945-04-12 | 
  10. | Kennedy    | John F.     | 1963-11-22 | 
  11. | Eisenhower | Dwight D.   | 1969-03-28 | 
  12. | Truman     | Harry S     | 1972-12-26 | 
  13. | Johnson    | Lyndon B.   | 1973-01-22 | 
  14. | Nixon      | Richard M.  | 1994-04-22 | 
  15. +------------+-------------+------------+ 
  16. 10 rows in set (0.00 sec) 

NULL字段的排在最前面,然后非NULL字段做升序排列

 

当我们对death做降序排序的时候

  1. mysql> select last_name, first_name, death from president order by  death desc;+------------+-------------+------------+ 
  2. | last_name  | first_name  | death      | 
  3. +------------+-------------+------------+ 
  4. | Ford       | Gerald R.   | 2006-12-26 | 
  5. | Reagan     | Ronald W.   | 2004-06-05 | 
  6. | Nixon      | Richard M.  | 1994-04-22 | 
  7. | Johnson    | Lyndon B.   | 1973-01-22 | 
  8. | Truman     | Harry S     | 1972-12-26 | 
  9. | Eisenhower | Dwight D.   | 1969-03-28 | 
  10. | Kennedy    | John F.     | 1963-11-22 | 
  11. | Roosevelt  | Franklin D. | 1945-04-12 | 
  12. | Carter     | James E.    | NULL       | 
  13. | Bush       | George H.W. | NULL       | 
  14. | Clinton    | William J.  | NULL       | 
  15. | Bush       | George W.   | NULL       | 
  16. +------------+-------------+------------+ 
  17. 12 rows in set (0.00 sec) 

NULL字段排在最后面

怎么才能够,在做降序排序的时候,NULL在前面,非NULL值在后面了,很明显death为NULL的是还没有去世的名单,别人给出的sql是:

  1. mysql> select last_name, first_name, death from president order by  if(death is null ,0 ,1) ,death desc
  2. +------------+-------------+------------+ 
  3. | last_name  | first_name  | death      | 
  4. +------------+-------------+------------+ 
  5. | Bush       | George W.   | NULL       | 
  6. | Clinton    | William J.  | NULL       | 
  7. | Bush       | George H.W. | NULL       | 
  8. | Carter     | James E.    | NULL       | 
  9. | Ford       | Gerald R.   | 2006-12-26 | 
  10. | Reagan     | Ronald W.   | 2004-06-05 | 
  11. | Nixon      | Richard M.  | 1994-04-22 | 
  12. | Johnson    | Lyndon B.   | 1973-01-22 | 
  13. | Truman     | Harry S     | 1972-12-26 | 
  14. | Eisenhower | Dwight D.   | 1969-03-28 | 
  15. | Kennedy    | John F.     | 1963-11-22 | 
  16. | Roosevelt  | Franklin D. | 1945-04-12 | 
  17. +------------+-------------+------------+ 
  18. 12 rows in set (0.00 sec) 

不知道怎么理解这个sql,求高人解答!