473,378 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

MySQL syntax wanted

Dormilich
8,658 Expert Mod 8TB
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
Apr 28 '09 #1
10 2290
Dormilich
8,658 Expert Mod 8TB
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
Apr 29 '09 #2
Dormilich
8,658 Expert Mod 8TB
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$$
Apr 29 '09 #3
r035198x
13,262 8TB
See the mysql refmanual for Insert and check if INSERT ... ON DUPLICATE KEY UPDATE will help.
Apr 30 '09 #4
Dormilich
8,658 Expert Mod 8TB
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.
Apr 30 '09 #5
r035198x
13,262 8TB
So how are you creating the new entry if the id does not exist?
Apr 30 '09 #6
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. CALL make_my_day();
that's what the Trigger is supposed to do.
Apr 30 '09 #7
r035198x
13,262 8TB
Why not use the insert/on duplicate key update instead of the trigger. I think that a trigger is overkill for this.
Apr 30 '09 #8
Dormilich
8,658 Expert Mod 8TB
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)
Apr 30 '09 #9
r035198x
13,262 8TB
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.
Apr 30 '09 #10
Dormilich
8,658 Expert Mod 8TB
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.
Apr 30 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
0
by: bobb | last post by:
(I'm a long term newbie :) ) I wanted to copy a blob's contents in mysql in python from one record to another. I had a heck of a time with it, so I wanted to post it for others. The where clause...
4
by: Spyros | last post by:
I want to move a MySQL Database 3.23.54-max-nt from a Windows 2000 Server to a 4.0.14-max on a Redhat Linux Server. Any idea will be welcome. Thanks Spyros
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
10
by: tuco357 | last post by:
This problem has been vexing me for some time and I thought I should consult the group.... Often times when writing a php script to handle some mysql DB transactions, I must write code that...
2
by: speralta | last post by:
My tired old eyes may be failing me, but the following insert statements look correct to me, but I can't seem to get a clean insert from a fairly large text file database into mysql. I was...
0
by: unixNAB | last post by:
MySQL 5.0.x Hello, I'm trying to work out but the MySQL reference guide to the grant syntax is a little off, it says... so i wanted to do something like grant all on %_log.* to .......;...
1
by: ajos | last post by:
hi evrybdy, the problem is:- i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem... when i enter values in the 2 text boxes and click enter...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.