Paul Wills
asked on
Mysql update statement or update if not exist
Hi Experts
I am in the process of creating a program that inserts data using multiselect forms. My question is, how do I do a column update without overwriting the exist data in a table column? Insert does not have a 'where' clause so I cannot use that.
I am in the process of creating a program that inserts data using multiselect forms. My question is, how do I do a column update without overwriting the exist data in a table column? Insert does not have a 'where' clause so I cannot use that.
ASKER
Hi Dan
Thank you for the feedback. What I am trying to achieve is update using a primary key and where clause. The reason for this is because the there is existing data in the table, when I use update it removes all values in the updated column, I want to add to existing data.
An example table is as follows:
f_id Primary key) fruits (varchar). The table fruits already contains fruits and would want to add more fruits in that table in a specific row using a primary and without deleting existing content.
Thanks
Thank you for the feedback. What I am trying to achieve is update using a primary key and where clause. The reason for this is because the there is existing data in the table, when I use update it removes all values in the updated column, I want to add to existing data.
An example table is as follows:
f_id Primary key) fruits (varchar). The table fruits already contains fruits and would want to add more fruits in that table in a specific row using a primary and without deleting existing content.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you please share some data so that it will be more clearer to help?
ASKER
Hi Dan
Thanks, I can perform the update from mysql commandline, but not through JDBC. I am using JDBC and preparedstatements for updates, I changed it a bit by adding a new character.
update fruits_tbl set fruitname=concat(fruitname , "\r\norange") where fruit_id='111';
The columns are as follows:
fruit_id (INT PRK) | fruitname (VARCHAR(255) | fruitdescr (VARCHAR(255))
Thanks
Thanks, I can perform the update from mysql commandline, but not through JDBC. I am using JDBC and preparedstatements for updates, I changed it a bit by adding a new character.
update fruits_tbl set fruitname=concat(fruitname
The columns are as follows:
fruit_id (INT PRK) | fruitname (VARCHAR(255) | fruitdescr (VARCHAR(255))
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much Dan, much appreciated, it works. I also made a few adjustments.
"update fruits_tbl set fruitname=concat(fruitname , \r\n'"+fruitname+"') where fruit_id="+fruit_id).
I have another question, but I post a new question for that.
Thanks.
"update fruits_tbl set fruitname=concat(fruitname
I have another question, but I post a new question for that.
Thanks.
INSERT IGNORE INTO table
This will simply generate a warning instead of an error if the key already exists and the INSERT will be discarded.
HTH,
Dan