Forum Moderators: open

Message Too Old, No Replies

Multi table MYSQL query

         

TheOne1337

9:31 am on Aug 18, 2007 (gmt 0)

10+ Year Member



I don't want to start new topic so i reply here my latest problem. I have fighted against this problem and i can't solve it.

Story: I have this private formula result quessing page. Where we are quessing formula comptetition results.

There is tables: users, games, bets.

Structure is like this:
users: id, username, ...
bets: user_id, game_id, .....
games: id, name, ...

And as you can understand, thing goes like this. If user named Example (ID: 4) is quessing results for game Turkish GP (ID: 10). Then bet goes to table bets. Where user_id is then 4 and game_id 10 and then there is hes/shes bets.

<?php
//Lets get all games from DB and already made bets counting.
$query_games = @mysql_query("
SELECT Count(B.game_id) AS kpl, G.*
FROM bets B
LEFT JOIN games G
ON B.game_id = G.id
GROUP BY G.id
ORDER BY G.id DESC");

//This is used to check is there any games on DB.
$check_games = @mysql_num_rows($query_games);

//Lets get all usernames from DB who hasn't made any bets to game. There is only 12 users so this isn't so heavy query.
//I have problem with this query. It doesn't work.
$nobets = @mysql_query("
SELECT G.username, Count(B.game_id) AS count
FROM users U, games G
LEFT JOIN bets B
ON (U.id = B.user_id AND B.game_id = B.id)
GROUP BY B.id
HAVING count=0
ORDER BY B.id DESC");

//Lets get busy
echo '
<h1>Open games</h1>

<table width="100%">
<tr>
<td width="40%"><b>Competition name</b></td>
<td width="20%"><b>Competition added</b></td>
<td width="16%"><b>Competition ends</b></td>
<td width="14%"><b>Bets</b></td>
<td width="9%"><b>Status</b></td>
</tr>';

while($f = @mysql_fetch_array($query_games))
{
//Beauty script. If number is smaller than 10 then add zero to front of number.
if($f['count'] < 10) { $count_bets = '0'.$f['count']; } else { $count_bets = $f['count']; }

$array = array("n" => array('<span class="locked">Locked</span>', '<span class="open">Open</span>', '<span class="closed">Closed</span>', '<span class="open">Results</span>'));

echo '
<tr>
<td><a href="?p=bet&id='.$f['id'].'">'.$f['username'].'</a></td>
<td>'.$f['start_datetime'].'</td>
<td>'.$f['end_datetime'].'</td>
<td title="';

echo 'Remaining bets: ';

//When you put your mouse over this cell. You can see usernames that hasn't made hes quess yet.
while($nobets_fetch = @mysql_fetch_array($nobets))
{
echo $nobets_fetch["username"].", ";
}

echo '">('.$count_bets.'/'.$count_users.')</td> <td>'.$array["n"][$f['status']].'</td>';
}

echo '</table>';
?>



My problem is making this $nobets query. Because it has to show remaining bets from same game what $f while fetch is showing. I can't get it work, i know that current $nobets query isn't working. And is this possible to do with only with 1 query?

SteveLetwin

9:03 pm on Aug 18, 2007 (gmt 0)

10+ Year Member



Does your nobets query return wrong results, zero rows, or an error?

It looks like you might have a couple typos in the query.

G.username -> U.username
B.game_id = B.id -> B.game_id = G.id

syber

9:38 pm on Aug 18, 2007 (gmt 0)

10+ Year Member



Your GROUP BY statements must match your SELECT columns lists.

If you are grouping on B.id, then B.id is the only non-aggregate column that can be in your SELECT column list (you have G.username in the column list).