cdoyle Posted January 18, 2009 Share Posted January 18, 2009 Hi, I'm trying to think how to accomplish this, but not really sure where to begin. I have my players table, that has all the players ID (id). I also have a medical ward table, for those who are currently in the hospital and can't play (player_id). What I would like to do is select random rows from the players table, but not include any players that are listed in the medical ward table. I started out with this, and then I remembered the medical ward players shouldn't be selected and that's where I got stuck. $getvictims = $db->execute("SELECT `id`, `username` FROM `players` WHERE `City_ID`=? ORDER BY RAND() Limit 20", array($player->City_ID)); Would I somehow use 2 queries, 1 query to select the players not in the medical ward, then another query to randomly select those from query 1? Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/ Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 <?php $getvictims = $db->execute("SELECT * FROM `players` WHERE `City_ID`=? and WHERE 'medicalward' != 'FALSE' ORDER BY RAND() Limit 20", array($player->City_ID)); ?> I ASSUME the field value medical ward is named such and has a setting of true or false not sure that will work or not but its my best off hand attempt Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740111 Share on other sites More sharing options...
cdoyle Posted January 19, 2009 Author Share Posted January 19, 2009 <?php $getvictims = $db->execute("SELECT * FROM `players` WHERE `City_ID`=? and WHERE 'medicalward' != 'FALSE' ORDER BY RAND() Limit 20", array($player->City_ID)); ?> I ASSUME the field value medical ward is named such and has a setting of true or false not sure that will work or not but its my best off hand attempt Thanks for replying, I'm not sure what you mean by a setting true or false? I have 2 different tables, `players` and `medical_ward` If a player is dead, their `id` from the `players` table, is listed in the `player_id` field in the `medical_ward` table. Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740115 Share on other sites More sharing options...
dropfaith Posted January 19, 2009 Share Posted January 19, 2009 oh my bad the two tables thing eluded me i was going from one table with a medical ward field your problem needs some sort of join which im not too clear on the workings of yet (still kinda noobish) Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740117 Share on other sites More sharing options...
fenway Posted January 19, 2009 Share Posted January 19, 2009 yes, you need a left join: select * from players as p left join medicalward as m on ( m.player_id = p.id ) where p.City_ID=? and m.player_id is null ORDER BY RAND() Limit 20 Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740247 Share on other sites More sharing options...
cdoyle Posted January 19, 2009 Author Share Posted January 19, 2009 yes, you need a left join: select * from players as p left join medicalward as m on ( m.player_id = p.id ) where p.City_ID=? and m.player_id is null ORDER BY RAND() Limit 20 thank you, that worked! I have another question related to this. Not really sure what to search for, but right now the query I have always returns 20 rows from the players table. Is there a way to make it so the number of rows it selects is dynamic? Meaning each time it runs, the total number of rows is different? For example, if I run it the first time it select say 15 rows. Then I run it again, it select 40 rows, the next time 8 rows. Not really sure what to search for on that. Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740792 Share on other sites More sharing options...
fenway Posted January 20, 2009 Share Posted January 20, 2009 You can use a random number generated from php.... Link to comment https://forums.phpfreaks.com/topic/141387-randomly-select-players-but-with-exclusions/#findComment-740982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.