PHP/MySQL Have some rows in SELECT result have different values for a particular column when sorting -
right sorry complicated title wasn't sure how best explain.
i have following query:
select * uc_posts `postinguser` = 5 , `id` not in (select dwable uc_redwables redwabledby = 5) union select * uc_posts id in (select dwable uc_redwables redwabledby = 5) order time desc limit 20 the table uc_posts looks this:
id | postinguser | time | message | 1 5 2016-08-08 23:50:45 * 2 5 2016-08-08 23:50:35 * 3 5 2016-08-08 23:50:25 * 4 5 2016-08-08 23:50:15 * the table uc_redwables looks this:
id | dwable | redwabledby | time | 1 3 5 2016-08-08 23:51:15 the output looks this:
id | postinguser | time | message | 1 5 2016-08-08 23:50:45 * 2 5 2016-08-08 23:50:35 * 3 5 2016-08-08 23:50:25 * 4 5 2016-08-08 23:50:15 * i'm trying find way substitute time in selected row(s) uc_redwables table , sort corresponding row (without updating row in uc_posts).
so this:
id | postinguser | time | message | 3 5 2016-08-08 23:51:15 * //time changed 1 5 2016-08-08 23:50:45 * 2 5 2016-08-08 23:50:35 * 4 5 2016-08-08 23:50:15 * can help? thanks!
try this:
select p.`id` id, p.`postinguser` postinguser, p.`message` message, case r.dwable when p.id r.`time` else p.`time` end date_time `uc_posts` p left join `uc_redwables` r on p.id = r.dwable p.`postinguser` = 5 , r.redwabledby = 5 order date_time desc; this should produce output like:
id | postinguser | date_time | message | 3 5 2016-08-08 23:51:15 * 1 5 2016-08-08 23:50:45 * 2 5 2016-08-08 23:50:35 * 4 5 2016-08-08 23:50:15 *
Comments
Post a Comment