Jump to content

executing the query once


php_begins

Recommended Posts

I have the following query where $userid is about 100,000 userids separated by commas.

 

<?
$getpostid=mysql_query("SELECT DISTINCT post.username,post.threadid,thread.threadid,thread.forumid from post,thread WHERE post.username='$username' AND post.threadid=thread.threadid AND post.userid IN ($userid) AND thread.forumid IN ($forumids)") or die (mysql_error());
?>

can i just print all the usernames from above result separated by ';' without looping it multiple times?

 

Could someone help me out how to do above in an efficient manner?

Link to comment
Share on other sites

It WILL take a lot of time.

 

From what I understand, the engine will check the entire table for each value within the IN() function. That's 100,000 table look-ups.

 

Why are you doing this? What are you trying to accomplish?

Link to comment
Share on other sites

    I need to see if the user has posted at least 1 post in one of these forums; 12, 9, 96, 176

    and last activity is more then six months ago, meaning they have not logged in within the last 6 months. and then print those usernames

  Given current table structure of post,thread,user and forum. This is the only way i could think of so far.Here is my complete code:

 

$forumids='12,9,96,176';
$getusername=mysql_query("SELECT userid from user WHERE posts >=1 AND lastactivity <='$newDate' ORDER by lastactivity") or die (mysql_error());
if(mysql_num_rows($getusername) > 0)
{
   $flag=1;
   while($getusername_result=mysql_fetch_assoc($getusername))
   {
      if($flag == 1)
      {
         $userid=$getusername_result['userid'];
         $flag=2;
      }
      else
      {
         $userid.=",".$getusername_result['userid'];
      }
   }
}
else
{
  //Nothin
}
echo $userid;
      //$getpostid=mysql_query("SELECT DISTINCT post.username,post.threadid,thread.threadid,thread.forumid from post,thread WHERE post.username='$username' AND post.threadid=thread.threadid AND post.userid IN ($userid) AND thread.forumid IN ($forumids)") or die (mysql_error());
      while($getpostid_result=mysql_fetch_assoc($getpostid))
      {

         $postusername=$getpostid_result['username'];
         echo $postusername."<br>";

      }


Link to comment
Share on other sites

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.