meman1188 Posted November 27, 2008 Share Posted November 27, 2008 I am running the query to get data out of table A (items). If there is a corresponding row present in table B (redirects) then I want to sort off of A.`update` otherwise i want to sort off of A.`original`. Table B is accessed by key ex (A.`key` = B.`key`). So this is sort of the sample i have right now but is not working: SELECT `items`.* , CASE WHEN EXISTS ( SELECT 1 FROM `redirects` WHERE `redirects`.`key` = `items`.`key` ) THEN `items`.`update` ELSE `items`.`original` END CASE AS `sort_by` FROM `items` WHERE (`items`.`key` = xxx) ORDER BY `sort_by`DESC Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/134446-mysql-sorting-based-on-if-row-exists-in-another-table/ Share on other sites More sharing options...
fenway Posted November 27, 2008 Share Posted November 27, 2008 You can use IF() or IFNULL() as appropraite. Quote Link to comment https://forums.phpfreaks.com/topic/134446-mysql-sorting-based-on-if-row-exists-in-another-table/#findComment-700507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.