jason white
jason white

Reputation: 711

unexpected result from IN query

The users_groups table

userid  groupid
10  4
10  2
6   4
11  4
12  2
13  2

  $query="SELECT count(*) FROM `users_groups` 
           WHERE `userid`='$fromuserid' 
             AND  `groupid` in (
               SELECT `groupid` 
                 FROM `users_groups` 
                WHERE `userid`=`$touserid
          );";

$fromuserid=10

$touserid=11

The query to find out userid 10 and userid 11's common groupid which is 4. but my query is returning null. Any mistake with the query?

Upvotes: 0

Views: 43

Answers (1)

Mark Byers
Mark Byers

Reputation: 837926

You have a syntax error in your query. There is an extra backtick in the subselect.

SELECT count(*)
FROM `users_groups`
WHERE `userid`='$fromuserid'
AND `groupid` in (SELECT `groupid` FROM `users_groups` WHERE `userid`=`$touserid)
--                                                                    ^
--                                                                   here

It should be this:

SELECT `groupid` FROM `users_groups` WHERE `userid`=$touserid

Upvotes: 3

Related Questions