2

Table:

userid, sessionid, last_active

I got the following requests:

SELECT `userid` FROM `session_keys` WHERE `sessionid`='SESSION_ID'

UPDATE `session_keys` SET `last_active`=now() WHERE `sessionid`='SESSION_ID'

Is it possible to put it in one request? but I need the callback from the select request.

The only goal i wanna reach is to update the last_active time of an timestamp when selecting the data.

Thank you.

  • I'm pretty sure you can, but I'm unclear what your exact request is. – user3783243 Jul 26 '18 at 23:30
  • You could put both statements inside a transaction so they're grouped and succeed or fail together. But generally SQL statements execute one at a time. What's the reason for wanting to group/combine the statements, out of curiosity? – RoboBear Jul 26 '18 at 23:32
  • I wanna update the last active time automatically when selecting the data... –  Jul 27 '18 at 10:31

3 Answers3

0

try:

SET @session_id = 'SESSION_ID';
SELECT `userid` INTO @sid FROM `session_keys` WHERE `sessionid` = @session_id;
UPDATE `session_keys` SET `last_active`=now() WHERE `sessionid` = @session_id;
SELECT @sid;

Could you give more context?

varTob
  • 124
  • 6
0

Try this query.

UPDATE
    'tableA' AS 'A',
    (
        SELECT
            *
        FROM
            'tableB'
        WHERE
            'id' = x
    ) AS 'src'
SET
    'A'.'col1' = 'src'.'col1'
WHERE
    'A'.'id' = x
;
Pang
  • 9,564
  • 146
  • 81
  • 122
Rajesh Om
  • 483
  • 2
  • 15
  • 39
0

Maybe this will be helpful:

Select and Update in same query (I know that you have the same table in both queries)

mysql select and update using one query in this case?

nmr
  • 358
  • 4
  • 10