php - Select the latest entry from two tables? -
now title might bit confusing, hard explain in few words.
so have table topics , posts. both have row "posted" represents time thread/post posted. need compare these , choose 1 entry , entry latest one.
i can kinda demonstrate mean. have now.
$postsq = $db->query("select t.posted, p.userid, p.topicid, t.id posts p, topics t t.forumid = '$forumsub[id]' , t.id = p.topicid order t.posted desc limit 1,1");
not best code know.
i know doesn't work show idea of want, here how query look
$postsq = $db->query("select (p.posted , t.posted) tpposted, p.userid, p.topicid, t.id posts p, topics t t.forumid = '$forumsub[id]' , t.id = p.topicid order tpposted desc limit 1,1");
anyone know how can achieve this?
you can union homogenised versions of tables together, , sort them in descending order posted
, limit result. try this:
select * ( select id, posted, userid, 'post' posts union select id, posted, userid, 'topic' topics ) q order posted desc limit 1;
with demonstration fiddle here: http://sqlfiddle.com/#!9/b46c2/1