MySQL query (for advanced users)

Discussion in 'Programming' started by hip_hop_x, Apr 28, 2009.

  1. #1
    Hello!
    I started with mySQL and i want to eliminate my cronjobs, and found about events.
    I want to create an event every 1 second, and to compare if endtime from table test < SELECT UNIX_TIMESTAMP(), then to set action='no' from the same table test.

    What i tried
    
    CREATE EVENT event ON SCHEDULE EVERY 1 SECOND DO
    BEGIN
    
    SELECT endtime,action FROM test IF(endtime<UNIX_TIMESTAMP(),SET action='no',NULL);
    
    END
    
    Code (markup):

    Can you please help me at this query?
     
    hip_hop_x, Apr 28, 2009 IP
  2. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    First of all you should not use the event as your event identifier, because it is the reserved keyword for th mysql
    database as you can see "CREATE EVENT"

    CREATE EVENT evnt ON SCHEDULE EVERY 1 SECOND DO
    BEGIN

    SELECT endtime,action FROM test IF(endtime<UNIX_TIMESTAMP(),SET action='no',NULL);

    END
     
    alfa_375, Apr 29, 2009 IP
  3. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #3
    Do not use event , your website's performance will reduce drastically.
     
    it career, Apr 29, 2009 IP
  4. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #4
    
    CREATE EVENT event ON SCHEDULE EVERY 1 SECOND DO
    BEGIN
    	UPDATE village_buildings SET done=1 WHERE(endtime<UNIX_TIMESTAMP());
    	
    	DECLARE ppid INT;
    	DECLARE action,build TEXT;
    	DECLARE curl CURSOR FOR SELECT name,town_id,action FROM village_buildings WHERE done=1;
    	
    	OPEN cur1;
    	  BEGIN
    		DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END;
    		LOOP
    		  FETCH cur1 INTO build,ppid,action;
    		  IF action == 'upgrade' THEN
    			UPDATE village_players_villages SET build=build+1 WHERE id=ppid;
    		  ELSE
    			UPDATE village_players_villages SET build=build-1 WHERE id=ppid;
    		  END IF;
    		END LOOP;
    	  END;
    	CLOSE cur1;
    END
    
    Code (markup):
    I found a way to fix that query but now i got more problems here, i get error at UPDATE village_buildings SET done=1 WHERE(endtime<UNIX_TIMESTAMP()); but if i run it alone it works fine.... can you help me fix this code?
    I know that if i'll run it at 1 second the website performance will be reduced, but i need to do this for a live system.
     
    hip_hop_x, Apr 30, 2009 IP