Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在同一查询中选择组成员计数和跟踪状态_Mysql_Sql_Database - Fatal编程技术网

Mysql 如何在同一查询中选择组成员计数和跟踪状态

Mysql 如何在同一查询中选择组成员计数和跟踪状态,mysql,sql,database,Mysql,Sql,Database,我试图得到用户的组,他创建或以下,连同总人数的追随者,并希望得到最新的追随者的日期,以便我可以通过与该命令。我有两张桌子 组 id title creator_id created 4 test3 123 1224322344 5 test2 213 2342344444 跟随 id to_id follower_id is_group created 1 4 222 1

我试图得到用户的组,他创建或以下,连同总人数的追随者,并希望得到最新的追随者的日期,以便我可以通过与该命令。我有两张桌子

id  title   creator_id   created
4   test3   123          1224322344
5   test2   213          2342344444
跟随

id  to_id  follower_id   is_group   created
1    4       222             1       234324324
1    4       123             1       444234234
1    5       213             1       234234444
我试过这个问题

SELECT g. * , IFNULL(f.total_followers, 0 ) total_followers 
FROM groups AS g
LEFT JOIN (

    SELECT to_id, COUNT( * ) AS total_followers
    FROM follow
    WHERE is_group =1
    GROUP BY to_id
) AS f ON ( g.id = f.to_id ) 
where g.creator=123 
GROUP BY g.id
g.created DESC
LIMIT 10
但它只返回用户自己组的关注者数量,而不是他正在关注的组的数量

我们将非常感谢您的帮助

谢谢

在以下情况下不使用:

SELECT g.* , IFNULL(f.total_followers, 0 ) total_followers, f.most_recent
FROM (SELECT * FROM groups where creator = 123
      UNION DISTINCT
      SELECT g.* FROM groups AS g1
      JOIN follow AS f1 ON ( g1.id = f1.to_id)
      WHERE f1.follower_id = 123) AS g
LEFT JOIN (
    SELECT to_id, COUNT( * ) AS total_followers, MAX(created) AS most_recent
    FROM follow
    WHERE is_group =1
    GROUP BY to_id
) AS f ON ( g.id = f.to_id )
ORDER BY most_recent DESC
LIMIT 10
在以下情况下不使用:

SELECT g.* , IFNULL(f.total_followers, 0 ) total_followers, f.most_recent
FROM (SELECT * FROM groups where creator = 123
      UNION DISTINCT
      SELECT g.* FROM groups AS g1
      JOIN follow AS f1 ON ( g1.id = f1.to_id)
      WHERE f1.follower_id = 123) AS g
LEFT JOIN (
    SELECT to_id, COUNT( * ) AS total_followers, MAX(created) AS most_recent
    FROM follow
    WHERE is_group =1
    GROUP BY to_id
) AS f ON ( g.id = f.to_id )
ORDER BY most_recent DESC
LIMIT 10

总的来说,我们从来没有在子句。这是一个性能指标。如果有更快的方法,我将不胜感激。除了使用in子句,我还能用其他方法吗?@DrewPierce是的,我通常会尽量避免使用它。对于这个查询来说,这是一种PITA,因为它在OR中——必须添加一个带有UNION的子查询。正如Barmar所说。问题是当“in(”打了一张大桌子。这对像“in”(“CA”,“UT”,“TX”)和“in”这样的人来说都是好的(“子句。这是一个性能指标。如果有更快的方法,我将不胜感激。除了使用in子句,我还能用其他方法吗?@DrewPierce是的,我通常会尽量避免使用它。对于这个查询来说,这是一种PITA,因为它在OR中——必须添加一个带有UNION的子查询。正如Barmar所说。问题是当“in(”点击一个大的表格。这对于类似“in”(“CA”,“UT”,“TX”)之类的东西是很好的