我不能让ORDER BY工作(日期类型)

时间:2012-11-29 19:46:48

标签: mysql

我正在尝试ORDER BY日期类型字段名称为dDate(格式示例为2008-12-26)但它不起作用,有人可以告诉我我做错了。

    $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 OUTPUT IS:

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)

非常感谢您的帮助..

1 个答案:

答案 0 :(得分: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

所以现在一切都很棒!!!!感谢大家!! :)