eevan79 Posted August 7, 2010 Share Posted August 7, 2010 Can I get total records from 3 tables with one query? This is my 3 queries: $result = mysql_query("SELECT * FROM ".$table_prefix."posts"); $result2 = mysql_query("SELECT * FROM ".$table_prefix."topics"); $result3 = mysql_query("SELECT * FROM ".$table_prefix."users"); And to get number of rows I use $topics = mysql_num_rows($result2); $posts = mysql_num_rows($result)-$topics; $users = mysql_num_rows($result3); Can I do this with one query? Quote Link to comment https://forums.phpfreaks.com/topic/210078-get-total-records-from-3-tables-with-one-query/ Share on other sites More sharing options...
abdfahim Posted August 8, 2010 Share Posted August 8, 2010 Something like this. Let say, 1st table has a column name t1_col1, 2nd table has a column name t2_col1, 3rd table has a column name t3_col1, $result = mysql_query("SELECT `t1_col1` as `col` FROM ".$table_prefix."posts UNION SELECT `t2_col1` as `col` FROM ".$table_prefix."topics UNION SELECT `t3_col1` as `col` FROM ".$table_prefix."users"); $topics = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/210078-get-total-records-from-3-tables-with-one-query/#findComment-1096506 Share on other sites More sharing options...
eevan79 Posted August 8, 2010 Author Share Posted August 8, 2010 Thanks for reply. Ok, I select all records with one query, but how to get total rows for each table? $topics = mysql_num_rows($result); Thats result for all tables. What is with posts and users separately? Quote Link to comment https://forums.phpfreaks.com/topic/210078-get-total-records-from-3-tables-with-one-query/#findComment-1096578 Share on other sites More sharing options...
fenway Posted August 10, 2010 Share Posted August 10, 2010 Stop -- don't ever use *, throw away all of the data, and just get a count. That's what COUNT(*) is for. Quote Link to comment https://forums.phpfreaks.com/topic/210078-get-total-records-from-3-tables-with-one-query/#findComment-1097301 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.