Autor Subiect: mysql stuff  (Citit de 22095 ori)

Admin

  • Administrator
  • guru
  • *****
  • Mesaje postate: 58
  • Karma: +0/-0
mysql stuff
« : Iunie 29, 2019, 01:59:29 p.m. »
//

 CREATE TABLE counters(
     id INT NOT NULL UNIQUE,   -- multiple counters can be stored in this table, this is its id
     value INT                            -- current value of the counter
  );
 
  -- Initialize the first counter with start value 10
  INSERT INTO counters VALUES (1, 10);
 
  -- Let's get the current value
  SELECT value FROM counters WHERE id = 1;
 
  -- Increment the counter
  UPDATE counters SET value = value + 1 WHERE id = 1;

//

START TRANSACTION;
 
  -- Let's get the current value
  SELECT value FROM counters WHERE id = 1 FOR UPDATE;
 
   -- Increment the counter
  UPDATE counters SET value = value + 1 WHERE id = 1;
 
  COMMIT;

//

UPDATE counters
SET value = (@cur_value := value) + 1
WHERE id = 1;

//

UPDATE counters
SET value = LAST_INSERT_ID(value) + 1
WHERE id = 1;