Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:53:09+00:00 2026-05-26T15:53:09+00:00

Just learning about sql joins and things, and I have a question. Can you

  • 0

Just learning about sql joins and things, and I have a question.

Can you JOIN on an update query? Or is it only for select ones. Because I have this code;

$five_ago = date("Y-m-d H:i:s", strtotime("$now - 5 mins"));

$sql_result23 = mysql_query("SELECT * FROM usersonline WHERE timestamp < '$five_ago'", $db);
while ($rs23 = mysql_fetch_array($sql_result23)) { 
    mysql_query("UPDATE users SET status='gone' WHERE id ='$rs23[userID]'"); 
}

It picks out from the usersonline table all the ones that are older than 5 minutes, then finds them in the users table and updates their record.

I’m not a JOIN wizard or anything but i think a join would simplify that.
Can someone clarify this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:53:09+00:00Added an answer on May 26, 2026 at 3:53 pm

    Using IN:

    UPDATE users 
    SET status='gone' 
    WHERE id IN 
          ( SELECT userID 
            FROM usersonline 
            WHERE timestamp < '$five_ago'
          )
    

    Using JOIN:

    UPDATE users 
      JOIN usersonline 
        ON usersonline.userID = users.id
    SET users.status='gone' 
    WHERE usersonline.timestamp < '$five_ago'
    

    Using EXISTS:

    UPDATE users 
    SET status='gone' 
    WHERE EXISTS  
          ( SELECT *
            FROM usersonline 
            WHERE timestamp < '$five_ago'
              AND userID = users.id
          )
    

    You could also skip the 5 minutes ago calculations in PHP and let the MySQL engine do that work, with:

    WHERE timestamp < NOW() - INTERVAL 5 MINUTE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning about generics and have a question regarding method return values. Say,
I am just learning about app.config in respect of creating custom sections. I have
I am learning about all things SQL, and upon reading about indexes I naturally
I've just begun learning about ColdFusion ORM and persistent cfcs and how it can
Just learning about mercurial's --style and --template options that can be used on hg
I am learning SQL and am trying to learn JOINs this week. I have
I'm learning SQL at the moment and I've read that joins and subqueries can
I'm learning more about SQL and have found it helpful already in solving a
I am just learning about the impressive SlickGrid library; and here's my question: We
So I am just learning .NET, WinForms, SQL Server and C#.... I previously have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.