Unix Technical Forum

Can I do this in one MySQL statement?

This is a discussion on Can I do this in one MySQL statement? within the MySQL forums, part of the Database Server Software category; --> I'm doing something here that can be done in two MySQL statements. Is it possible to do it in ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-14-2008, 03:25 AM
John Nagle
 
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-14-2008, 03:25 AM
Dan Rumney
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-14-2008, 03:25 AM
John Nagle
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:29 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com