Jump to content

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.