I got some code that need to look through three tables to display some information for a minecraft top list.
The tables are the following:
servers
id | user_id | name | information | websitename | websiteurl | postdate.. etc
This is the main table that contains server name, website information(Full description of the server) etc.
vote
id | server_id | username | ipaddress | votetimestamp
On the website I allow players to vote every 24 hour. All votes get inserted into this table with the users in-game name(username), the server id and then time of the vote.
ping
id | server_id | min_player | max_player | motd | pingtimestamp
This table get updated by another script every 10 mins with CronJobs that is running on my web server with the use of fsock’s.
Doing this I can find out if the server is offline or online, how many players there is online and how many players that can be online at a time.
On my index page I got a script that should pull out data from all three tables onto the web page and display every server in the database order after the server that got the most votes to the server that got the least votes.
I can pull out every server that already got a vote in the voting table however if there is a server that haven’t received a vote yet it wont get listed which is should.
This is the SQL code I use.
SELECT DISTINCT(ping.server_id), COUNT(vote.server_id) AS count, servers.id,
servers.name, servers.server_ip, ping.min_player,ping.max_player,ping.motd
FROM servers,vote,ping
WHERE servers.id = vote.server_id
AND servers.id = ping.server_id
GROUP BY servers.id
ORDER BY count DESC
LIMIT $start, $per_page
I’m sure this is simple enough but I’ve tried a few things now but nothing really seem to work.
Would be a good idea to mention at this point that SQL is not really my strong suit.
Edit
I have tried to remove the ´DISTINCT´ in the string but for some reason it returns multiple rows of every server which is displaying the server more then once.
Every server should only be displayed once, sorting from top to bottom after the server that got the most votes to the least.
First, don’t use the comma-delimited syntax where you simply list the tables in your From clause; instead use the Join syntax. It would help if you showed us some sample inputs and expected outputs. You stated that you want all servers whether or not they have a vote means you will probably need to use Left Joins:
You were not clear on the structure and purpose of the
pingtable nor the nature of the results if there are multiple rows in thepingtable for the sameservers.idvalue. In the above example, I guessed as to the aggregate functions to use forping.min_player,ping.max_playerandping.motd. In addition, I assumed you really did want all servers rows and not necessarily those that contained a value in thepingtable and so I used a Left Join fromserverstoping.In addition, if
servers.idis the primary key of theserverstable and since you are using MySQL, you do not need to enumerate all of its columns in the Group By (but you would in other database products). Since it wasn’t stated whetherservers.idis the primary key, I enumerated them.