473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Need help with MySql query

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
`subjects`,`subject`,`totalbytes`,`date`,`groups`, `num`,`max`,`setgroup`)

VALUES (last_insert_id(), ?subject, ?totalbytes, ?date, ?groups, ?num,
?max, ?setgroup) on duplicate key update totalbytes = totalbytes +
?totalbytes,num = num + 1";
cmds.Prepare();
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);
cmds.ExecuteNonQuery();

Aug 12 '06 #1
1 2503
ad***@binindex.net wrote:
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.
If setgroup is your primary key for this table, there are a couple of
solutions in MySQL:

http://dev.mysql.com/doc/refman/5.0/en/replace.html
http://dev.mysql.com/doc/refman/5.0/...duplicate.html

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
column.

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.

Regards,
Bill K.
Aug 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Julien - Marseille | last post by:
Hello, I need help for php syntax when i call Mysql database I have wrote that and my sql connection is working I just have a problem with this command line : $query = "SELECT * FROM...
2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
0
by: Jaime Teng | last post by:
Hi, I have a table: mysql> describe archivetable; +-----------+----------+------+-----+---------------------+-------+ | Field | Type | Null | Key | Default | Extra |...
0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
2
by: Jeffrey D Moncrieff | last post by:
I am trying to create a data base for colleting date for my research project. I have run in to a couple of problems and I need it fast ASAP I need it to be able to add data and edit them and view...
2
by: m.k.ball | last post by:
Thanks Rich - that's great. Before I found this group, I thought I had a reasonable understanding of SQL (well, MySQL's implementation of it, at least) but the truth is there are great chunks that...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
4
by: SBmx | last post by:
hello I need help building a SELECT query... well, two queries actually i have three tables: products / fields id, name, type, country producttypes / fields: id, name countries / fields:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.