Jump to content

Recommended Posts

Well I've done this before I'm just not remembering how...Takes too much thinking for me right now :P

 

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"...

Link to comment
https://forums.phpfreaks.com/topic/206208-mysql-order-by-another-table/
Share on other sites

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.

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

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?

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...
}

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...
}

<?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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.