3 sql inner joined tables, now how to count total rows in them
Question
here is the query, I have inner joined 3 tables, Now i am looking to count all rows in the result i.e. 78 by executing query on php my admin, I don't want result of table, i jst want 1 row in wchich only thing written is 78, same as we do in
SELECT count (*) FROM test_table
Below is the query of 3 inner joined tables
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
now how to count total number of rows in it ?
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
Error ::#1054 - Unknown column 'mybb_users.uid' in 'field list'
Solution
well, if you don't want the result, why are you showing those fields ? A select return one table, so apply count to it:
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
Also, you're groupying by posts, so, don't need to order
Edit: I think the query I've posted should work, anyway, if it's not, this definitely should work:
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow