Php 如何在对大多数行进行计数时加入SQL表?

Php 如何在对大多数行进行计数时加入SQL表?,php,mysql,sql,Php,Mysql,Sql,所以我有一个“bookmark”表,我正试图获取书签内容最多的用户的“UserID”。此查询返回十个最活跃用户的UserID。此查询在我看来是这样的,并且有效: SELECT COUNT(*) AS `Rows`, UserID FROM `bookmark` WHERE UserID NOT IN(1, 2, 25, 38, 41, 43, 47, 125) GROUP BY UserID ORDER BY `Rows` DESC LIMIT 10 现在,我尝试将结果查询连接到“acco

所以我有一个“bookmark”表,我正试图获取书签内容最多的用户的“UserID”。此查询返回十个最活跃用户的UserID。此查询在我看来是这样的,并且有效:

SELECT COUNT(*) AS `Rows`, UserID FROM `bookmark`
WHERE UserID NOT IN(1, 2, 25, 38, 41, 43, 47, 125) 
GROUP BY UserID ORDER BY `Rows` DESC LIMIT 10 
现在,我尝试将结果查询连接到“accounts”表,通过将UserID与accounts表中的“ID”进行比较来获取每个用户ID的所有信息

SELECT COUNT(*) AS `Rows`, UserID FROM `bookmark`
join accounts ON accounts.ID = bookmark.UserID
WHERE UserID NOT IN(1, 2, 25, 38, 41, 43, 47, 125) 
GROUP BY UserID ORDER BY `Rows` DESC LIMIT 10 
这不会将任何行作为accounts表返回,而是返回与以前相同的结果。我需要能够获取accounts表的所有行(与使用*)


如果my accounts表中有这些行=用户名、id、电子邮件,my bookmark表中有这些行=id、用户id、链接,这可能很难看,但您可以将第一个查询存储在临时表中,然后在帐户和临时表之间进行连接


另外,试着做一个左连接,看看你是否得到了这样的结果。有时这有助于调试查询。

这可能很难看,但您可以将第一个查询存储在临时表中,然后在帐户和临时表之间进行连接


另外,试着做一个左连接,看看你是否得到了这样的结果。有时这有助于调试查询。

一种解决方案是将
分组按
移动到子查询:

select  *
from    (
        select  count(*) as Rows
        ,       UserID 
        from    bookmark
        where   UserID not in (1, 2, 25, 38, 41, 43, 47, 125) 
        group by
                UserID 
        ) as BookmarkSubquery
join    accounts
on      accounts.ID = BookmarkSubquery.UserID
order by
        BookmarkSubquery.Rows DESC 
limit   10 
另一种方法是通过以下方式将accounts中的列添加到组中:

select  count(*) as Rows
,       bookmark.UserID 
,       accounts.Name
,       accounts.BirthDate
from    bookmark
join    accounts
on      accounts.ID = BookmarkSubquery.UserID
where   bookmark.UserID not in (1, 2, 25, 38, 41, 43, 47, 125) 
group by
,       bookmark.UserID 
,       accounts.Name
,       accounts.BirthDate
order by
        count(*) DESC 
limit   10 

注意:MySQL只允许您将
书签.UserID
列在
分组依据中
;尽管这样做可行,但不建议这样做。

一种解决方案是将
分组按
移动到子查询:

select  *
from    (
        select  count(*) as Rows
        ,       UserID 
        from    bookmark
        where   UserID not in (1, 2, 25, 38, 41, 43, 47, 125) 
        group by
                UserID 
        ) as BookmarkSubquery
join    accounts
on      accounts.ID = BookmarkSubquery.UserID
order by
        BookmarkSubquery.Rows DESC 
limit   10 
另一种方法是通过以下方式将accounts中的列添加到组中:

select  count(*) as Rows
,       bookmark.UserID 
,       accounts.Name
,       accounts.BirthDate
from    bookmark
join    accounts
on      accounts.ID = BookmarkSubquery.UserID
where   bookmark.UserID not in (1, 2, 25, 38, 41, 43, 47, 125) 
group by
,       bookmark.UserID 
,       accounts.Name
,       accounts.BirthDate
order by
        count(*) DESC 
limit   10 

注意:MySQL只允许您将
书签.UserID
列在
分组依据中
;尽管这样做可行,但不建议这样做。

您的选择列表中没有包含其他表中的任何列。您没有从“accounts”表中选择任何内容。将第一行更改为选择COUNT(*)作为'Rows',UserID来自'bookmark',*来自'accounts`meh-我对此进行了辩论-但这几乎肯定是解决方案。OP可以计算要显示的列。您的选择列表中没有包含其他表中的任何列。您没有从“accounts”表中选择任何内容。将第一行更改为选择COUNT(*)作为'Rows',UserID来自'bookmark',*来自'accounts`meh-我对此进行了辩论-但这几乎肯定是解决方案。OP可以计算要显示的列。