2012-11-29 45 views
1

我想按日期類型字段命名字段名稱是dDate(格式示例是2008-12-26),但它不起作用有人可以告訴我我做錯了什麼請。我不能得到ORDER BY工作(日期類型)

$listsql = "SELECT contact.id, CONCAT(lastname,', ',firstname) AS fullname, lastname, firstname, 
       dDate, line1, line2, city, state, zip, phone1, phone2, country, whoAdded 
       FROM ". TABLE_CONTACT ." AS contact 
       LEFT JOIN ". TABLE_ADDRESS. " AS address ON contact.id=address.id AND contact.primaryAddType=address.type 
       LEFT JOIN ". TABLE_EMAIL ." AS email ON contact.id=email.id AND contact.primaryAddType=email.type 
       WHERE contact.hidden != 1 
       ORDER BY email.dDate DESC"; 

它不提供任何錯誤或任何東西,它只是不排隊日期,我想它有日期排隊DESC或ASC並不重要 例如:

2008-12-26 
2008-12-27 
2008-12-28 

OR

2008-12-28 
2008-12-27 
2008-12-26 

我得到這樣的

2012-11-16 
2012-11-30 
2012-11-26 
2012-11-30 

SQL輸出爲:

mysql> show create table address_email; 
+---------------+--------------------------------------------------------------- 
-------------------------------------------------------------------------------- 
-------------------------+ 
| Table   | Create Table 

         | 
+---------------+--------------------------------------------------------------- 
-------------------------------------------------------------------------------- 
-------------------------+ 
| address_email | CREATE TABLE `address_email` (
    `id` int(11) NOT NULL DEFAULT '0', 
    `dDate` date DEFAULT NULL, 
    `type` varchar(100) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 | 
+---------------+--------------------------------------------------------------- 
-------------------------------------------------------------------------------- 
-------------------------+ 
1 row in set (0.00 sec) 

非常感謝你的幫助..

+3

什麼不行?它做了什麼,它不應該或不應該這樣做?不瞭解您的模式,上面的代碼是正確的。 –

+0

@MichaelBerkowski它沒有提供任何錯誤或任何它只是沒有排隊的日期我希望有日期排隊DESC或ASC無關緊要 – compcobalt

+0

請添加一個[SQLFiddle](http://sqlfiddle.com)的例子。 –

回答

0

我改變:

LEFT JOIN ". TABLE_EMAIL ." AS email ON contact.id=email.id AND contact.primaryAddType=email.type 

到:

LEFT JOIN ". TABLE_EMAIL ." AS email ON contact.id=email.id 

我想永遠不會有使用兩個LEFT JOIN時的兩個primaryAddType的

所以現在所有的作品很棒!感謝大家!! :)