RJP1 Posted February 21, 2011 Share Posted February 21, 2011 Hi guys, I was wondering the best way to do this... I'd like to output the result of number_x divided by number_y along with the data that is in Table 1 in ASC order of the division result. Is that easily doable? Table 1: id | userid | name | email | number_x Table 2 id | userid | number_y Thanks for any help guys, would be very useful! RJP Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/ Share on other sites More sharing options...
pets2soul Posted February 21, 2011 Share Posted February 21, 2011 I would say...LEFT JOIN both tables...like this: SELECT table1.number_x, table2.number_y FROM table1 LEFT JOIN table2 ON table1.userid = table2.userid ORDER BY table1.id ASC Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177467 Share on other sites More sharing options...
RJP1 Posted February 21, 2011 Author Share Posted February 21, 2011 Hmm, I think I got my question wrong last night... What I need it to do is this: Table 1: id | userid | name | email | number_x Table 2 id | userid Count the number of records in table2 that have the same userid as table1, then divide number_x by this count and then output all data from table 1 (with the calculation) in ASC order... That's was what I meant because the number_y doesn't exist as such, it is actually the count of rows in the second table. Cheers, RJP Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177571 Share on other sites More sharing options...
kickstart Posted February 21, 2011 Share Posted February 21, 2011 Hi This should do it:- SELECT id, userid, name, email, number_x, number_x / bCount AS CalcField FROM (SELECT a.id, a.userid, a.name, a.email, a.number_x, COUNT(b.id) AS bCount FROM Table1 a INNER JOIN Table2 b ON a.userid = b.userid GROUP BY a.id, a.userid, a.name, a.email, a.number_x) fred ORDER BY CalcField You might be able to do it in one go without the subselect but not certain. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177593 Share on other sites More sharing options...
RJP1 Posted February 21, 2011 Author Share Posted February 21, 2011 Thanks kickstart that seems to work! By the way, what is the "fred" about? Cheers! RJP1 Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177596 Share on other sites More sharing options...
kickstart Posted February 21, 2011 Share Posted February 21, 2011 Hi When joining with a subselect you need to give it an alias name even if you don't use that alias name. I just called it fred to give it a name. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177598 Share on other sites More sharing options...
RJP1 Posted February 21, 2011 Author Share Posted February 21, 2011 Nice, thanks a lot Keith, this has helped me to learn what can be done no end! Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/228330-divide-a-value-in-one-table-by-a-value-in-another-table/#findComment-1177601 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.