Jump to content

Get total records from 3 tables with one query


eevan79

Recommended Posts

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?

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.