1

I want to find out which user is not part of other games available in database i get an array as following

$arr2=Array ( (0) => Array ( (uid) => 1, (game_id) => 22 ), (1) => Array ( (uid) => 2, (game_id) => 22 ) (2) => Array ( (uid) => 1, (game_id) => 23 ) (3) => Array ( (uid) => 3, (game_id) => 24 ) ); 

For example User 1 is in Game 22,23 BUT NOT IN 24 user 3 is in only game 24 I want to find out which user is not participating in other game, Issue is effeciency(speed of execution) and how to represent in array so as i can use it to display.Is it god idea to have it like userid=> notInGame,notInGame (CSV)?

1
  • Yes i can... but dont know what sql query to trigger as i want only single query to run on a page to optimize it for speed of execution Commented Jul 11, 2011 at 6:56

2 Answers 2

1

An example of how to do it in PHP:

$array = array( array ( 'uid' => 1, 'game_id' => 22 ), array ( 'uid' => 2, 'game_id' => 22 ), array ( 'uid' => 1, 'game_id' => 23 ), array ( 'uid' => 3, 'game_id' => 24 ), ); $games = $users = array(); foreach($array as $value) { $games[] = $value['game_id']; $users[$value['uid']][] = $value['game_id']; } foreach($users as $uid => $user) { $users[$uid] = array_diff($games, $user); } print_r($users); 

Result (key is the uid):

Array ( [1] => Array ( [3] => 24 ) [2] => Array ( [2] => 23 [3] => 24 ) [3] => Array ( [0] => 22 [1] => 22 [2] => 23 ) ) 
Sign up to request clarification or add additional context in comments.

Comments

0

Below is quite simple query if You don't need id of groups. The num field is a number of groups the user is in.

SELECT `uid`, COUNT(*) AS `num` FROM `a_table` GROUP BY `uid`; 

You will get something like this

$arr2=Array ( (0) => Array ( (uid) => 1, (num) => 2 ), (1) => Array ( (uid) => 2, (num) => 1 ) (2) => Array ( (uid) => 3, (num) => 1 ) ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.