计算MySQL中的连接值

时间:2014-01-16 10:51:47

标签: mysql

我正在生成一个查询,我正在使用GROUP_CONCAT获取逗号分隔的用户ID列表。我想在同一个查询中计算这些ID。我可以这样做吗?

$query="SELECT id, 
            longitude, 
            latitude, 
            game_date, 
            min_player, 
            game_description, 
            is_public, 
            is_user_coming, 
            allow_player_invite, 
            location, 
            game_type, 
            game_status, 
            cdate, 
            ownerid, 
            COUNT(j.users) as joinees,
            users.username
            FROM games 
            left join 
            (SELECT gameid, GROUP_CONCAT(userid, ',') as users 
            from user_game_join where games.id=user_game_join.gameid) j on j.gameid=id 
            join (select id as uid,name as username from users) users on users.uid=ownerid 
            AND (`location` LIKE '$location%' or `location` LIKE '".ucfirst($location)."%')";

这是我的查询,我需要获得木工的数量。随附的是我的表的快照: user_game_join

games

users

2 个答案:

答案 0 :(得分:1)

SELECT gameid, 
       GROUP_CONCAT(userid, ',') as users,
       count(userid) as user_count
from user_game_join 
where games.id = user_game_join.gameid

答案 1 :(得分:-1)

我的一个朋友帮助了我。令人惊讶的是,我在错误的地方使用join。共享查询以防万一有人可能会发现它有用:

SELECT games.*,ugj.joinees,u.username FROM games JOIN 
(select id as uid, name as username from users) users on users.uid = games.ownerid 
AND games.id='$gid' left join 
(select count(userid) as joinees,gameid as gid from user_game_join group by gameid ) ugj on games.id=ugj.gid LEFT JOIN 
(select id,name as username from users) u on u.id=games.ownerid