zackcez Posted June 29, 2010 Share Posted June 29, 2010 Well I've done this before I'm just not remembering how...Takes too much thinking for me right now I have this: $result = $db->query_read("SELECT * FROM server_status ORDER BY up DESC"); And this function: function getVotes($id) { $gVotes = mysql_query("SELECT * FROM server_votes WHERE vote='1' AND server_id='" . $id . "'"); $bVotes =mysql_query("SELECT * FROM server_votes WHERE vote='-1' AND server_id='" . $id . "'"); $goodVotes = mysql_num_rows($gVotes); $badVotes = mysql_num_rows($bVotes); return ($goodVotes - $badVotes); } I know that function is ugly but at the moment that's not what I care about. I'd like to know how I can have it order by "up" (which is either a 1 or a 0) and then order by "getVotes"... Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/ Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 ANd what's the relation between these tables? Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078829 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 One holds the servers data and the other one holds the servers ranking or "votes"... Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078830 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 SELECT s.*, v.score FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v USING (server_id) ORDER BY s.up, v.score Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078833 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 Would you explain that for me if you could please? Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078838 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 There's a subquery: SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id you can run it on its own to see it returns sum of votes for each server and server_id. This result is then joined with server_status table using server_id column. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078839 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 Alright, thanks; But how exactly does the cross join syntax work...I've never used that before ;s. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078842 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 It creates a Cartesian product of both sets of data limited to elements that meet the condition (in this case both elements need to have equal values in server_id column) Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078848 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 I'm not sure if this would matter, but within server_status the "server_id" column is actually called "owner_id". Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078850 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 It would. We need to change join condition SELECT s.*, v.score FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078851 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 so it would look like this :s? $result = $db->query_read("SELECT s.*, v.score FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score DESC"); What would I need to change in order to make it work with my fetch_array then? Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078854 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 I don't know. I don't know what $db is. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078856 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 $db is the database class of vbulletin...it's just a normal mysql_query pretty much. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078858 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 Then just use it as normal mysql_query. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078860 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 Sorry but...I thought I had it but it doesn't output anything :s: $result = mysql_query("SELECT * FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score DESC"); while($row = mysql_fetch_array($result)) { echo $row['server_name']; // That's a column in the server_status table... } Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078866 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 Let's add some debugging code then and see if I made a mistake somewhere: $result = mysql_query("SELECT * FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score DESC") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['server_name']; // That's a column in the server_status table... } Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078867 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 <?php $result = mysql_query("SELECT * FROM server_status AS s CROSS JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score DESC") or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['server_name']; // That's a column in the server_status table... echo("hmm"); } ?> Nothing at all :s. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078871 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 Can you run this query in PhpMyAdmin or similar tool just to check if it runs fine? Also, please familiarise yourself with: http://www.phpfreaks.com/tutorial/debugging-a-beginners-guide/page1 Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078880 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0020 sec ) Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078884 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 That's something. Apprently there are no rows meeting this set of conditions. HOw about this? SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078886 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 I just found out why it wasn't work...If there's no votes for that server in the table, then it doesn't do anything...is there anyway I can just have it return zero...Sorry I didn't even think of that I thought there was still a few rows in it :s. Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078887 Share on other sites More sharing options...
Mchl Posted June 29, 2010 Share Posted June 29, 2010 Sure there is. That's what LEFT JOINS are for SELECT s.*, IFNULL(v.score,0) AS score FROM server_status AS s LEFT JOIN (SELECT server_id, SUM(IF(vote=1,1,0))-SUM(IF(vote=-1,1,0)) AS score FROM server_votes GROUP BY server_id) AS v ON s.owner_id = v.server_id ORDER BY s.up, v.score DESC Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078890 Share on other sites More sharing options...
zackcez Posted June 29, 2010 Author Share Posted June 29, 2010 Thank you very much, it's all working good now . Quote Link to comment https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/#findComment-1078891 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.