![]() |
|
|
#1 |
|
Junior Member
Join Date: May 2005
Posts: 9
|
Conditional actions
I need to keep a running count of actions for particular dates, so I need to say 'if this date & value aren't in the table, insert them. if they are, increment them'. Is this possible to do in 1 query? I can't figure how.
Here's what I'm doing at the minute: Code:
Code:
Code:
This seems pretty inefficient to me. Can it be done in one query? btw i'm using version 4.0 |
|
|
|
|
|
#2 | |
|
Senior Member
Join Date: Jul 2004
Posts: 167
|
Re: Conditional actions
Quote:
Look very carefully into the REPLACE INTO syntax. It may solve your problem. But you have to really understand it well. It works based on the index. Do you know how to do multiple columns per index? REPLACE INTO will overwrite an existing line if it exists or INSERT a new one if it doesn't exist. But it is based on the unique index. I'm not even 100% sure it would work. Hm. The more I think about it the less I think it will work. I'm not sure you can really save any steps. Where is the bottleneck? Is it just that you want to save a step or two? Or are you doing some heavy duty logging where you call those three lines a huge number of times? Because if it's the latter there are other techniques you can do to achieve improved performance. But to give you that I'd need a little more detail on what you're doing and what your goal is. How critical it is for each hit to be counted. Because if losing a few hits isn't critical to you I may suggest you use a table in memory. But if losing a bit of data when the mysql server goes down periodically then I'll stuck w/ a regular table.
__________________
** Most misspellings intentional to combat spaham filterz** |
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: May 2005
Posts: 9
|
Re: Conditional actions
I've figured out the the answer is to use ON DUPLICATE KEY UPDATE.
I made a unique key out of concatenating other fields, then used the following: Code:
Works like a charm. |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating a conditional query | gcornish | SQL syntax | 8 | 08-16-2004 12:24 AM |