Connecting Tech Pros Worldwide Forums | Help | Site Map

MySQL syntax wanted

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#1: Apr 28 '09
Hi,

basicly I want to increase a counter variable for a given "id" in a MySQL table (using UPDATE)
Expand|Select|Wrap|Line Numbers
  1. _stats (id, date, hits, visits)
this won't work, if the entry does not exist. currently I'm checking for the existence of the "id" before I update and create a new entry if the "id" doesn't exist.

my question now, is there a way to accomplish that in one query?

thanks

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Apr 29 '09

re: MySQL syntax wanted


after som asking and consulting google it seems like I need a combination of TRIGGER and a stored procedure…

I suppose, I only need to declare (create) the trigger/stored procedure once, but I'm not sure (I couldn't find a clear statement for that, though the examples imply that), could someone clarify that?

looking further into it

Dormi
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#3: Apr 29 '09

re: MySQL syntax wanted


I'm having trouble with the trigger
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER 
  2.     add_today
  3. BEFORE UPDATE
  4. ON 
  5.     `_stats`
  6. FOR EACH ROW
  7.     BEGIN
  8.         CALL count_dates(@dates); // this procedure works
  9.         IF
  10.             @dates = 0
  11.         THEN
  12.             CALL make_my_day(); // this procedure works too
  13.     END$$ // $$ is set as temporary delimiter
I get the message

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 13 (line numbers are equivalent)
further, phpmyadmin shows a error sign on the IF … THEN … construct.

anyone an idea what's wrong here?

info: MySQL 5.0.45 via UNIX socket, phpMyAdmin 2.11.9.1

EDIT: the stored procedures:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE
  2.     count_dates (OUT param INT)
  3.     BEGIN
  4.         SELECT
  5.             COUNT(`date`)
  6.         FROM
  7.             `_stats`
  8.         WHERE
  9.             `date` = CURDATE()
  10.         INTO
  11.             param;
  12.     END$$
  13.  
  14. CREATE PROCEDURE
  15.     make_my_day ()
  16.     BEGIN
  17.         INSERT INTO
  18.             `_stats`
  19.             (`id`, `date`, `visits`, `hits`)
  20.         VALUES
  21.             ('', CURDATE(), 0, 0);
  22.     END$$
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Apr 30 '09

re: MySQL syntax wanted


See the mysql refmanual for Insert and check if INSERT ... ON DUPLICATE KEY UPDATE will help.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#5: Apr 30 '09

re: MySQL syntax wanted


I eventually figured out an END IF statement was missing (well, I'm not yet finished testing)

ON DUPLICATE KEY UPDATE won't help me here, since I don't use INSERT in the first place.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Apr 30 '09

re: MySQL syntax wanted


So how are you creating the new entry if the id does not exist?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#7: Apr 30 '09

re: MySQL syntax wanted


Expand|Select|Wrap|Line Numbers
  1. CALL make_my_day();
that's what the Trigger is supposed to do.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Apr 30 '09

re: MySQL syntax wanted


Why not use the insert/on duplicate key update instead of the trigger. I think that a trigger is overkill for this.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#9: Apr 30 '09

re: MySQL syntax wanted


that may be true, but
- I always want to learn new stuff
- when I initially asked about the problem, the only answer I got was "you can try a trigger" and I've had not heard about the insert … on duplicate key update before (I'm just familiar with the basic update/insert/select/delete stuff)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Apr 30 '09

re: MySQL syntax wanted


If it's for learning then ok. Triggers do place a bit of overhead on the database and I would want to minimize them as much as possible.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#11: Apr 30 '09

re: MySQL syntax wanted


thanks for the info. unfortunately, the MySQL manual is not a very exhaustive one, I had quite some difficulty getting all the info I needed.
Reply