Mike Tuller <[EMAIL PROTECTED]> wrote on 09/02/2004 16:53:00:

> I have posted this question a few times, and have not seen the answer
that I
> need.
>
> I have a shell script, that gathers information from systems, and I want
> that info to be entered into a database. I want it to check first to see
if
> the data is already entered, and if not, add it. If it has already been
> entered, then update the record.
>
> I would think that some type of if/else statement would work, but I can't
> get the IF statement http://www.mysql.com/doc/en/IF_Statement.html to
work
> correctly in MySql.
>
> Here is what I have:
>
> "IF SELECT * FROM hardware_assets WHERE
ethernet_address='$ethernet_address'
> IS NULL\
>     THEN INSERT into hardware_assets (ethernet_address) VALUES
> ($ethernet_address)\
> ELSE\
>     UPDATE hardware_assets SET operating_system='10.3.3'\
> END IF;"

You probably want the REPLACE command (
http://www.mysql.com/doc/en/REPLACE.html )
which provides exactly this functionality:

REPLACE INTO hardware_assets VALUES ($ethernet_address, ....) ;

where the ethernet_address column has been specified to be UNIQUE.

The MySQL IF is an operator, not a flow control structure: It would map
more closely to the C "A?x:y" operator rather than the " if () then {} else
{}".

      Alec


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to