3 sql内连接表,现在如何计算它们中的总行数

时间:2012-08-25 15:06:19

标签: php mysql mybb

这里是查询,我有内部加入3个表,现在我想要计算结果中的所有行,即78通过执行查询我的管理员,我不想要表的结果,我想要在wchich中只有一行写的是78,和我们在

中一样
SELECT count (*) FROM test_table

以下是3个内连接表的查询

        SELECT 
mybb_users.uid,
mybb_users.username,
mybb_users.avatar,
mybb_posts.fid,
mybb_posts.uid,
mybb_posts.dateline,
mybb_posts.tid,
mybb_posts.subject,
mybb_forums.parentlist,
mybb_forums.fid
FROM mybb_forums
    INNER JOIN mybb_posts ON mybb_forums.fid = mybb_posts.fid
    INNER JOIN mybb_users ON mybb_posts.uid = mybb_users.uid
    WHERE mybb_forums.parentlist LIKE '%58%'
    GROUP BY mybb_posts.tid
    ORDER BY mybb_posts.dateline DESC

现在如何计算其中的总行数?

EDITED

 SELECT count( mybb_users.uid ) AS totalOfRows
FROM (

SELECT mybb_users.uid, mybb_users.username, mybb_users.avatar, mybb_posts.fid, mybb_posts.uid AS uidPost, mybb_posts.dateline, mybb_posts.tid, mybb_posts.subject, mybb_forums.parentlist, mybb_forums.fid AS fidForum
FROM mybb_forums
INNER JOIN mybb_posts ON mybb_forums.fid = mybb_posts.fid
INNER JOIN mybb_users ON mybb_posts.uid = mybb_users.uid
WHERE mybb_forums.parentlist LIKE '%58%'
GROUP BY mybb_posts.tid
)T

错误::#1054 - 未知栏' mybb_users.uid'在'字段列表'

1 个答案:

答案 0 :(得分:1)

好吧,如果您不想要结果,为什么要显示这些字段?选择返回一个表,因此对其应用计数:

SELECT 
count(*)
FROM mybb_forums
    INNER JOIN mybb_posts ON mybb_forums.fid = mybb_posts.fid
    INNER JOIN mybb_users ON mybb_posts.uid = mybb_users.uid
    WHERE mybb_forums.parentlist LIKE '%58%'
    GROUP BY mybb_posts.tid

此外,您按帖子进行分组,因此,无需订购

编辑: 我认为我发布的查询应该可行,无论如何,如果不是,这肯定应该有效:

SELECT sum(Total) AS totalOfRows 
FROM ( 

SELECT 1 as Total 
FROM mybb_forums 
INNER JOIN mybb_posts ON mybb_forums.fid = mybb_posts.fid 
INNER JOIN mybb_users ON mybb_posts.uid = mybb_users.uid 
WHERE mybb_forums.parentlist LIKE '%58%' 
GROUP BY mybb_posts.tid 
)T