Go Back   Rhinocerus > Newsgroup > Newsgroup comp.databases.mysql

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2008, 08:44 PM
John Nagle
Guest
 
Posts: n/a
Default Can I do this in one MySQL statement?

I'm doing something here that can be done in two MySQL statements.
Is it possible to do it in one?

queuestate = 'queued" ## Python
'EXISTS SELECT * WHERE ip_hash=%',(iphash,) ## Already on queue?
if existed, queuestate = 'hold' ## if so, set to hold
'INSERT INTO ratingqueue
(domain, requestor_ip_hash, priority,
rating_state, request_timestamp)
VALUES (%s,%s,%s,queuestate,NOW())',
(domain, iphash, priority, queuestate)) ## add to queue

The general idea is that if no record exists with an ip_hash of iphash,
the record being inserted is listed as "queued". Otherwise, it's listed
as "hold".

This is being done with the MEMORY engine, so I can't use transactions.
I prefer to avoid LOCK TABLE if possible on this table, which is
being used to queue transactions going through multiple servers.

I think a nested query would be illegal because it's from the same
table as the one being updated. Is that right in this case?

John Nagle

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-13-2008, 08:50 PM
Dan Rumney
Guest
 
Posts: n/a
Default Re: Can I do this in one MySQL statement?

John Nagle wrote:
> I'm doing something here that can be done in two MySQL statements.
> Is it possible to do it in one?
>
> queuestate = 'queued" ## Python
> 'EXISTS SELECT * WHERE ip_hash=%',(iphash,) ## Already on queue?
> if existed, queuestate = 'hold' ## if so, set to hold
> 'INSERT INTO ratingqueue
> (domain, requestor_ip_hash, priority,
> rating_state, request_timestamp)
> VALUES (%s,%s,%s,queuestate,NOW())',
> (domain, iphash, priority, queuestate)) ## add to queue
>
> The general idea is that if no record exists with an ip_hash of iphash,
> the record being inserted is listed as "queued". Otherwise, it's listed
> as "hold".
>

[snip]

is iphash a key?

If so,

INSERT ... ON DUPLICATE KEY UPDATE

might be what you're looking for

http://dev.mysql.com/doc/refman/5.0/...duplicate.html
Reply With Quote
  #3 (permalink)  
Old 06-14-2008, 12:49 AM
John Nagle
Guest
 
Posts: n/a
Default Re: Can I do this in one MySQL statement?

Dan Rumney wrote:
> John Nagle wrote:
>> I'm doing something here that can be done in two MySQL statements.
>> Is it possible to do it in one?
>> queuestate = 'queued" ## Python
>> 'EXISTS SELECT * WHERE ip_hash=%',(iphash,) ## Already on queue?
>> if existed, queuestate = 'hold' ## if so, set to hold
>> 'INSERT INTO ratingqueue
>> (domain, requestor_ip_hash, priority,
>> rating_state, request_timestamp)
>> VALUES (%s,%s,%s,queuestate,NOW())',
>> (domain, iphash, priority, queuestate)) ## add to queue
>>
>> The general idea is that if no record exists with an ip_hash of iphash,
>> the record being inserted is listed as "queued". Otherwise, it's listed
>> as "hold".
>>

> [snip]
>
> is iphash a key?
>
> If so,
>
> INSERT ... ON DUPLICATE KEY UPDATE
>
> might be what you're looking for
>
> http://dev.mysql.com/doc/refman/5.0/...duplicate.html


Cute feature, but doesn't help. I want to insert a new row,
but I want it tagged. ON DUPLICATE KEY UPDATE doesn't insert a
new row; it just modifies the existing one.

Logically, you should be allowed a subquery on INSERT when
you're only inserting one row (the usual case), because there's no
sequencing issue. But MySQL doesn't implement that.

John Nagle
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch() Statement Question Jujitsu Lizard Newsgroup comp.lang.c 148 04-13-2009 03:17 PM
Re: SUM statement paradox (bug)? Tim Kynerd Newsgroup comp.soft-sys.sas 0 03-13-2009 08:36 PM
Re: Automatic Retain Statement when using Set statement to Vijayan Sundaram Newsgroup comp.soft-sys.sas 0 07-21-2008 03:36 PM
REMOVE Matt Sparks Newsgroup comp.soft-sys.sas 0 12-06-2006 02:29 PM



All times are GMT. The time now is 10:01 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.