Jump to content

[MySql] Order By Another Table...


zackcez

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
Share on other sites

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

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.