我有2个表 - chats_topices和chats_replies
我想从最接近当前日期的chats_replies中选择所有chats_topices字段+字段createDate(timestamp)。
chats_replies包含连接表
之间的字段CHATID$sql = "SELECT *
FROM `chats_topics` AS topics, `chats_replies` AS replies
WHERE (".$search.") AND
(topics.id = replies.chatID) AND
(XXXX)
ORDER BY topics.createDate DESC";
答案 0 :(得分:1)
在实时代码中使用SELECT *是不好的做法,我会选择这样的东西;
SELECT topics.Field1, topics.Field2, MAX(replies.createDate) createDate
FROM `chats_topics` AS topics
LEFT JOIN `chats_replies` AS replies
ON topics.id = replies.chatID
WHERE (".$search.")
AND (topics.id = replies.chatID)
AND (XXXX)
GROUP BY topics.Field1, topics.Field2
ORDER BY topics.createDate DESC
在您选择的字段中添加所需的字段,然后将其添加到组中。
答案 1 :(得分:1)