MySQL syntax wanted  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| |
Hi,
basicly I want to increase a counter variable for a given "id" in a MySQL table (using UPDATE) - _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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: MySQL syntax wanted
I'm having trouble with the trigger - CREATE TRIGGER
-
add_today
-
BEFORE UPDATE
-
ON
-
`_stats`
-
FOR EACH ROW
-
BEGIN
-
CALL count_dates(@dates); // this procedure works
-
IF
-
@dates = 0
-
THEN
-
CALL make_my_day(); // this procedure works too
-
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: - CREATE PROCEDURE
-
count_dates (OUT param INT)
-
BEGIN
-
SELECT
-
COUNT(`date`)
-
FROM
-
`_stats`
-
WHERE
-
`date` = CURDATE()
-
INTO
-
param;
-
END$$
-
-
CREATE PROCEDURE
-
make_my_day ()
-
BEGIN
-
INSERT INTO
-
`_stats`
-
(`id`, `date`, `visits`, `hits`)
-
VALUES
-
('', CURDATE(), 0, 0);
-
END$$
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | re: MySQL syntax wanted
See the mysql refmanual for Insert and check if INSERT ... ON DUPLICATE KEY UPDATE will help.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | 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
| | | re: MySQL syntax wanted
So how are you creating the new entry if the id does not exist?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: MySQL syntax wanted
that's what the Trigger is supposed to do.
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | 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
| | | 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | 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.
|  | Similar MySQL Database bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|