Need help with MySql query

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have this query which works to insert and update the row.  Problem is

I need it to check and see if a value in the row is the same as the one

being set if it is then do the update part if not then don't.  I need
to check setgroup.  If the one in the row matches the one of the insert
then do the update otherwise don't.

Here is my code for the insert update.

cmds.CommandText = "INSERT INTO

VALUES (last_insert_id(), ?subject, ?totalbytes, ?date, ?groups, ?num,
?max, ?setgroup) on duplicate key update totalbytes = totalbytes +
?totalbytes,num = num + 1";

                cmds.Parameters.Add("?subject", heads.RealSubject);
                cmds.Parameters.Add("?totalbytes", heads.TotalBytes);
                cmds.Parameters.Add("?date", heads.Date);
                cmds.Parameters.Add("?groups", heads.Groups);
                cmds.Parameters.Add("?num", "1");
                cmds.Parameters.Add("?max", heads.MaxMessages);
                cmds.Parameters.Add("?setgroup", group);

Re: Need help with MySql query wrote:
Quoted text here. Click to load it

If setgroup is your primary key for this table, there are a couple of
solutions in MySQL:

However, in both of these cases, they only check for cases of duplicated
primary key value.  They do not check for existing values in a different

So I think you'll have to do this as a multi-step process: use a SELECT
query to find rows where `setgroup` is a certain value, and do your
insert only if none are returned in that result set.

Not every problem can be implemented in a single SQL statement.

Bill K.

Site Timeline