blinks Posted April 10, 2011 Share Posted April 10, 2011 As the title says I want to retrieve the highest value from 2 tables where C1 = 'Bob'. Based on the mock-up tables below it should grab 50 from Table 2. Table 1 C1C2 Bob48 Will62 Table 2 C1C2 Bob50 Will59 Quote Link to comment https://forums.phpfreaks.com/topic/233296-retrieving-maximum-value-from-2-tables/ Share on other sites More sharing options...
blinks Posted April 10, 2011 Author Share Posted April 10, 2011 Sorry, I should have said - I'd appreciate help with creating an SQL query to retrieve the highest value for Bob, whether it be in Table 1 or Table 2. The queries I create keep coming back as "undefined". TIA Quote Link to comment https://forums.phpfreaks.com/topic/233296-retrieving-maximum-value-from-2-tables/#findComment-1199807 Share on other sites More sharing options...
kickstart Posted April 11, 2011 Share Posted April 11, 2011 Hi Something like this should do it SELECT MAX(C2) FROM (SELECT C2 FROM Table1 WHERE C1 = 'Bob' UNION SELECT C2 FROM Table2 WHERE C1 = 'Bob') AliasName All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/233296-retrieving-maximum-value-from-2-tables/#findComment-1199970 Share on other sites More sharing options...
blinks Posted April 11, 2011 Author Share Posted April 11, 2011 Thanks Keith; that works beautifully when I run the SQL alone. Packaged up with some PHP, it is coming back as undefined, so I guess the problem must be with my PHP. Thanks heaps for your help! Quote Link to comment https://forums.phpfreaks.com/topic/233296-retrieving-maximum-value-from-2-tables/#findComment-1200272 Share on other sites More sharing options...
kickstart Posted April 12, 2011 Share Posted April 12, 2011 Hi Post the php code and we can have a look to see if there is a problem there. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/233296-retrieving-maximum-value-from-2-tables/#findComment-1200424 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.