Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PERL MYSQL query "IF EXISTS then UPDATE else INSERT"

285 views
Skip to first unread message

S Pratap Singh

unread,
Oct 20, 2010, 2:37:03 AM10/20/10
to begi...@perl.org
Hello,

I want to update my database if the key value is already there then it will
update the required field else it will insert the new row in the same table.


Insert query works fine but I am not able to figure out how to achieve this
"IF EXISTS then UPDATE else INSERT" using a one liner MYSQL statement . If
someone can give me an example to write such query and execute it from perl
then it would be really appreciated.

Thank you.
Pratap

Chris Knipe

unread,
Oct 20, 2010, 3:26:15 AM10/20/10
to Uri Guttman, S Pratap Singh, begi...@perl.org
Just use SQL?

INSERT INTO tbl, VALUES ('11','22','33') ON DUPLICATE KEY ....

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

On Wed, Oct 20, 2010 at 9:23 AM, Uri Guttman <u...@stemsystems.com> wrote:

> >>>>> "SPS" == S Pratap Singh <kdar...@gmail.com> writes:
>
>
> SPS> Insert query works fine but I am not able to figure out how to
> SPS> achieve this "IF EXISTS then UPDATE else INSERT" using a one
> SPS> liner MYSQL statement . If someone can give me an example to
> SPS> write such query and execute it from perl then it would be really
> SPS> appreciated.
>
> that is an sql question that has nothing to do with perl. please ask it
> in an sql forum. the answer that i know is that you can't do it in basic
> sql.
>
> uri
>
> --
> Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com--
> ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com---------
>
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>


--

Regards,
Chris Knipe

Uri Guttman

unread,
Oct 20, 2010, 3:23:09 AM10/20/10
to S Pratap Singh, begi...@perl.org

Peter Scott

unread,
Oct 20, 2010, 8:33:04 AM10/20/10
to begi...@perl.org

This isn't a Perl question, you're asking in the wrong place. This is
analogous to asking your auto mechanic what the feeding habits of emus
are because you used your car to drive to the zoo.

--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl1/

Mimi Cafe

unread,
Oct 20, 2010, 10:38:00 AM10/20/10
to S Pratap Singh, begi...@perl.org
This is the wrong list for MySQL questiosn, but I believe you are probably
looking to replace the matching record, use MySQL "INSERT REPLACE INTO
tablename....".

Mimi


=> -----Original Message-----
=> From: S Pratap Singh [mailto:kdar...@gmail.com]
=> Sent: 20 October 2010 07:37
=> To: begi...@perl.org
=> Subject: PERL MYSQL query "IF EXISTS then UPDATE else INSERT"
=>
=> Hello,
=>
=> I want to update my database if the key value is already there then it
=> will
=> update the required field else it will insert the new row in the same
=> table.
=>
=>
=> Insert query works fine but I am not able to figure out how to achieve
=> this
=> "IF EXISTS then UPDATE else INSERT" using a one liner MYSQL statement
=> . If
=> someone can give me an example to write such query and execute it from
=> perl
=> then it would be really appreciated.
=>
=> Thank you.
=> Pratap

0 new messages