Ivo San
Ivo San

Reputation: 1253

Update multiple rows with different conditions

I found a lot of similar questions here, but sorry to ask this again because I couldn't get one on how to implement them to the case I have.

here's mine

$upd = "UPDATE users SET friend_requests=CONCAT(friend_requests, ' $_GET[user]') WHERE uid='$_SESSION[uid]', ";
$upd = mysql_query($upd) or die(mysql_error());

$upd = "UPDATE users SET friends_waiting=CONCAT(friends_waiting, ' $_SESSION[uid]') WHERE uid='$_GET[user]'";
$upd = mysql_query($upd) or die(mysql_error()); 

How can I update the row in just one query?

Upvotes: 0

Views: 197

Answers (1)

Raul
Raul

Reputation: 607

UPDATE users 
  SET friend_requests=
    CASE
        WHEN uid='$_SESSION[uid]' THEN CONCAT(friend_requests, ' $_GET[user]')    
        ELSE friend_requests
        END,
      friends_waiting=
    CASE 
        WHEN uid='$_GET[user]' THEN CONCAT(friends_waiting, ' $_SESSION[uid]')    
        ELSE friends_waiting
        END
  WHERE uid IN ($_GET[user], $_SESSION[uid])

Upvotes: 1

Related Questions