urgent - how to select village count with single query - clan

Discussion in 'PHP' started by G3n3s!s, Feb 26, 2011.

  1. #1
    Hello - I am creationg my own game but I am stucked when selecting clan members. I got this query

    $members = $db->query("SELECT users.username, count(villagedata.vid) as villcount, users.id, villagedata.name, villagedata.pop FROM users,villagedata WHERE users.cid = ".$cid." GROUP BY users.id");

    But count(villagedata.vid) is giving me 6 instead of 2. (6 is total amount of villages on whole server).
    Can you tell me how should I select only count of villages JUST FOR THAT ONE USER ?
     
    Last edited: Feb 26, 2011
    G3n3s!s, Feb 26, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    
    SELECT username, (SELECT COUNT(vid) FROM villagedata) as villcount, id, FROM users WHERE cid = ".$cid." GROUP BY id
    
    PHP:
     
    tvoodoo, Feb 26, 2011 IP
  3. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    but I need village count for only ONE CERTAILY SELECTED user :(

    It means

    SELECT username,COUNT_OF_HIS_VILLAGES, id FROM users WHERE cid=".$cid." GROUP by id

    Thanks anyways
     
    G3n3s!s, Feb 26, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    SELECT u.username, (SELECT COUNT(v.vid) FROM villagedata as v WHERE v.column_for_user = u.id) as villcount, u.id, FROM users AS u WHERE u.cid = ".$cid." GROUP BY u.id
     
    tvoodoo, Feb 26, 2011 IP
  5. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Okay, that's better
    I got this

    $members=$db->query("SELECT u.username, (SELECT COUNT(v.vid) FROM villagedata as v WHERE v.owner_id = u.id) as villcount, v.pop, u.id FROM users AS u, villagedata as v WHERE u.cid = ".$cid." GROUP BY u.id");

    And it works! I was so stupid lol. Thank you
     
    G3n3s!s, Feb 27, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Your welcome !
     
    tvoodoo, Feb 27, 2011 IP