带有CONCAT的mySQL DATE日期和时间不起作用

时间:2015-02-23 19:57:42

标签: mysql date concat

我在数据库中有两列,一个是eDate,另一个是eTime。我试图在mySQL查询中得到它,我可以加入它们来制作一个完整的DATETIME,结合起来只在NOW()之后产生记录。

这是数据图片: enter image description here

这是我正在使用的代码:

$sql = "SELECT *,
      (SELECT count(*) FROM aw_event_attendees AS attendees WHERE events.eid = attendees.eid) AS allattending,
      (SELECT count(*) FROM aw_event_attendees AS attendees WHERE events.eid = attendees.eid AND attendees.uid = " . $_POST['uid']. " ) AS attending
FROM aw_events as events WHERE DATE(CONCAT(eDate, ' ', eTime)) >= NOW() ORDER BY events.eid DESC";

这是我得到的结果:

enter image description here

今天缺席,23-2-2015 @ 21:30。我似乎无法让它意识到时间。有什么建议吗?

由于

2 个答案:

答案 0 :(得分:0)

我强烈建议将对象存储为DATETIME对象,并在需要的地方将它们分开,而不是拆分它们并尝试加入。

事件具有特定的DATETIME,因此它是正确的数据类型。

答案 1 :(得分:0)

您可以将代码更改为:

SELECT *,
      (SELECT count(*) FROM aw_event_attendees AS attendees WHERE events.eid = attendees.eid) AS allattending,
      (SELECT count(*) FROM aw_event_attendees AS attendees WHERE events.eid = attendees.eid AND attendees.uid = 35) AS attending
FROM aw_events as events WHERE STR_TO_DATE(CONCAT(eDate, ' ', eTime), '%Y-%m-%d %H:%i:%s') > NOW() ORDER BY events.eid DESC

代码更改如下:

STR_TO_DATE(CONCAT(eDate, ' ', eTime), '%Y-%m-%d %H:%i:%s')

结果仅在NOW()之后给出日期和时间,包括日期是否相同:

enter image description here

完成工作!希望它能帮助别人:)