我目前正在尝试制作一个活动流列表:
帖子、评论都是混合在一起的,并且是按日期排序的我的想法是提出两个问题,但我不知道如何把它们混淆起来。以下是我的疑问:
// Query the posts :
$queryPosts = "
SELECT * FROM $wpdb->posts
WHERE post_type = \'post\'
AND post_status = \'publish\'
ORDER BY post_date DESC
";
// Query the comments :
$queryComments = "
SELECT * FROM $wpdb->comments
ORDER BY comment_date DESC
";
使用某种SQL联接是否可能做到这一点?
UPDATE:
我尝试了@scribu关于使用SQL UNION的建议,效果很好:
SELECT ID AS entry_id, post_date AS entry_date, post_content AS entry_content FROM $wpdb->posts
WHERE post_type = \'post\'
AND post_status = \'publish\'
UNION
SELECT comment_ID AS entry_id, comment_date AS entry_date, comment_content AS entry_content FROM $wpdb->comments
ORDER BY entry_date DESC
我现在要做的是访问posts表中的一些数据,而不是comments表中的数据。有什么想法吗?
提前谢谢。