SocomNegotiator Posted April 17, 2008 Share Posted April 17, 2008 Well I have some games that I am querying...here is the code SELECT id,game FROM ladders ORDER BY game LIMIT 1 Now when I say limit 1 it actually limits it to one result. Well I want to limit it to one result for each game. Because I have more than one game, and some games are listed more than once. So I don't need the same game to come up twice. Any ideas what I can put...? Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/ Share on other sites More sharing options...
mrdamien Posted April 17, 2008 Share Posted April 17, 2008 SELECT DISTINCT game, id FROM ladders will pick one of each game. You can filter out the results with WHERE conditions. Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519166 Share on other sites More sharing options...
SocomNegotiator Posted April 17, 2008 Author Share Posted April 17, 2008 Hmmm....it still returns every game. You got another idea? Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519177 Share on other sites More sharing options...
SocomNegotiator Posted April 17, 2008 Author Share Posted April 17, 2008 Hey when I take out id it works. Is there a way I can keep id in? I need to declare it so I can't leave it out. Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519186 Share on other sites More sharing options...
benphp Posted April 17, 2008 Share Posted April 17, 2008 If ID is unique, and it sounds like it, then select distinct will return everything. You could select ID within your sql results SELECT DISTINCT game FROM ladders while { SELECT id FROM ladders where game = $dbgame LIMIT 1 } This is conceptual of course. Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519195 Share on other sites More sharing options...
mrdamien Posted April 17, 2008 Share Posted April 17, 2008 SELECT id, game FROM ladders WHERE game IN ( SELECT DISTINCT game FROM ladders ) try that Edit: wait, no im getting confused. Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519222 Share on other sites More sharing options...
mrdamien Posted April 17, 2008 Share Posted April 17, 2008 Its generally faster to query only once. This is still less efficient than the query you want, but I can't figure this one out: <? $results = mysql_query('SELECT id, game FROM ladders'); $games = array(); while($row = mysql_fetch_assoc($results)){ if(in_array($row['game'], $games)){ // Do nothing, already used }else{ // Do stuff. $games[] = $row['game']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519245 Share on other sites More sharing options...
SocomNegotiator Posted April 17, 2008 Author Share Posted April 17, 2008 That did work...thanks a lot man!!! Quote Link to comment https://forums.phpfreaks.com/topic/101498-solved-help-with-php-query/#findComment-519880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.