Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php sql update join

Tags:

join

php

mysql

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?

like image 569
user961882 Avatar asked Nov 02 '11 23:11

user961882


People also ask

Can we use join in update SQL?

In SQL Server, you can use these join clauses in the UPDATE statement to perform a cross-table update. In this syntax: First, specify the name of the table (t1) that you want to update in the UPDATE clause. Next, specify the new value for each column of the updated table.

Can we use join in update query MySQL?

JOIN clause in MySQL is used in the statement to retrieve data by joining multiple tables within a single query. The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition.

Can we join two tables in update query?

It is possible to join two or more tables in an UPDATE query.

How can I update data from one table to another in MySQL?

UPDATE syntax: UPDATE table_name SET column_name = value WHERE condition; To perform the above function, we can set the column name to be equal to the data present in the other table, and in the condition of the WHERE clause, we can match the ID.


2 Answers

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
like image 156
ypercubeᵀᴹ Avatar answered Sep 30 '22 12:09

ypercubeᵀᴹ


Yes, you can JOIN in an UPDATE statement, but I would probably use the IN () subquery as suggested elsewhere, as I find the syntax more straightforward than the awkward JOIN below:

UPDATE users 
  JOIN usersonline ON users.id = usersonline.userid
  SET users.status='gone'
WHERE usersonline.timestamp < DATE_SUB(NOW(), INTERVAL 5 MINUTE);

Note also the use of MySQL's own DATE_SUB() so you don't have to handle that in PHP beforehand.

like image 25
Michael Berkowski Avatar answered Sep 30 '22 12:09

Michael Berkowski