Jump to content

[SOLVED] Problem querying mysql


padams

Recommended Posts

I'm trying to filter information from my database, displaying results based on what a user selects. They choose the team on a previous page, and this is sent to this page via GET. The query involves COUNT, JOIN, GROUP BY and ORDER BY, and works fine until I add the WHERE part. The tries table has a column called teamID so I thought I would be able to simply add the WHERE part in and it would filter out only those records where the teamID matches the one selected by the user. Unfortunately something isn't working!

 

if (isset($_GET['team'])) {

$team = $_GET['team'];

} else {

$team = 1;

}

 

$tries = "SELECT tries.playerID, players.playerFirstName, players.playerLastName, COUNT(tries.playerID) as triesscored FROM tries JOIN players ON tries.playerID = players.playerID GROUP BY tries.playerID WHERE tries.teamID = '".$team."' ORDER BY triesscored DESC";

$tries_query = mysql_query($tries) or die(mysql_error());

$rsTries = mysql_fetch_assoc($tries_query);

Link to comment
Share on other sites

Sorted it out at last. The WHERE part needs to appear before the GROUP BY.

 

$tries = "SELECT tries.playerID, players.playerFirstName, players.playerLastName, COUNT(tries.playerID) as triesscored FROM tries JOIN players ON tries.playerID = players.playerID WHERE tries.teamID = '".$team."' GROUP BY tries.playerID ORDER BY triesscored DESC";

Link to comment
Share on other sites

Can you post the error,

 

you may also  try

 

$tries = "
SELECT tries.playerID, players.playerFirstName, players.playerLastName, COUNT(tries.playerID) as triesscored 
FROM tries 
JOIN players ON tries.playerID = players.playerID 
WHERE tries.teamID = '".$team."' 
GROUP BY tries.playerID 
ORDER BY triesscored DESC";

 

OR

 

$tries = "
SELECT tries.playerID, players.playerFirstName, players.playerLastName, COUNT(tries.playerID) as triesscored, players.playerID 
FROM tries, players 
WHERE tries.playerID = players.playerID  AND tries.teamID = '".$team."' 
GROUP BY tries.playerID 
ORDER BY triesscored DESC";

 

EDIT: D'oh too slow!

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.