Tandem Posted August 30, 2006 Share Posted August 30, 2006 Hi. I'm trying to use records from two tables to try to make a sort of score board. I'll try to explain the situation as thoroughly as possible.Table1 has the following fields: Username, Number1Table 2 has the following fields: Username, Number2Every user has an entry into both tables. I want to add up Number1 and Number2 for every user, and then order in descending order, so i can make the score board from this informationHow would i go about doing that?Any help appreciated. Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/ Share on other sites More sharing options...
Barand Posted August 30, 2006 Share Posted August 30, 2006 Try[code]SELECT a.username , (a.number1 + b.number2) as totalFROM table1 a INNER JOIN table2 b ON a.username = b.usernameORDER BY total DESC[/code] Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/#findComment-83180 Share on other sites More sharing options...
Tandem Posted September 2, 2006 Author Share Posted September 2, 2006 Thanks for the reply.What if the situation changed. Let's say that not all players had records in Table 2, but i wanted the results to show who has the total number.So for example if somebody had no record in table 2 nut his 'Number 1' was higher than anyone elses, and was higher than anyone elses Number 1 and Number 2 combined, they would be at the top.Please say if this doesn't make sense. Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/#findComment-84772 Share on other sites More sharing options...
Barand Posted September 2, 2006 Share Posted September 2, 2006 Change INNER JOIN to LEFT JOIN Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/#findComment-84774 Share on other sites More sharing options...
Tandem Posted September 2, 2006 Author Share Posted September 2, 2006 Again, thanks for the reply.When i change it to LEFT JOIN i still get the same result.Let me try to explain again, as i missed a few words and made a few typos in my last post.I'm just going to say it how it is instead of theoretical :PThis is for a game where players can earn 'cash'. I have two tables, one called MEMBERS, and one called BANK. Every member has a record in MEMBERS. One of the fields in MEMBERS is CASH, which is how much 'cash' they have. Members can choose to put their 'cash' in the 'bank', and upon doing so will have a record added to the table BANK. The field from this table where there 'cash' is stored is called BANKED. I want to create a score board showing who has the most total cash in descending order. So for the players who have records in BANK, there total would be CASH + BANKED, and everyone elses total is just CASH. I hope this makes sense. Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/#findComment-84805 Share on other sites More sharing options...
Barand Posted September 2, 2006 Share Posted September 2, 2006 try[code]<?php$sql = "SELECT m.username , CASE WHEN b.banked IS NULL THEN m.cash ELSE (m.cash + b.banked) END as total FROM member m LEFT JOIN bank b ON m.username = b.username ORDER BY total DESC";$res = mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($res)) { echo $row['username'], ' ', $row['total'], '<br/>';}?>[/code] Link to comment https://forums.phpfreaks.com/topic/19215-2-tables-1-query/#findComment-84879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.