How can I UPDATE certain fields in a table?

[expired user #3565]'s profile image [expired user #3565] posted 16 years ago in General Permalink
I tried the "Import CSV file" feature...
I wanted to replace only the values in fields of one column,
but that deleted the values in all other columns... :-(
I noticed, that import generated this sql query:
LOAD DATA LOCAL INFILE 'D:\\1aDesktop\\dataset-table_field-data2_1.csv' REPLACE INTO TABLE `form4`.`dataset` FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' (`dataset_id`,`data2_1`)

Previously I had success with inserting this sql comand
directly in the SQL tab:
UPDATE `dataset` SET `data1_5`='my data blah'

that updated all fields in the "data1_5" column with the same data and that was what I needed.

But now I need to update the fields individually.
I've read, there must be additionally a "WHERE" command in that sql query to achieve that.
But I can't figure out, how exactly this WHERE command must be in order to make it work?

Can someone please help?

Ideally I would like to load the data from a csv file and then
update the fields.
So, I guess the sql query would similar to this:
LOAD DATA LOCAL INFILE 'D:\\1aDesktop\\dataset-table_field-data2_1.csv' FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' UPDATE `dataset` SET `data2_1` WHERE `data2_1`= ......????


Thanks.
ansgar's profile image ansgar posted 16 years ago Permalink
See http://dev.mysql.com/doc/refman/5.0/en/load-data.html

There is no "UPDATE mode" in LOAD DATA. This is the syntax:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[FIELDS
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]
ansgar's profile image ansgar posted 16 years ago Permalink
Alternative: Write an import routine in PHP or whatever which reads the CSV file and updates your table like you need it.
[expired user #3565]'s profile image [expired user #3565] posted 16 years ago Permalink
so, how can I update the fields individually *without* using a file?
How have I to change this:
UPDATE `dataset` SET `data1_5`='my data blah'

i.e. what is the correct WHERE statement that I must add there?

I know, it must be something like this:
UPDATE `dataset` SET `data1_5`='my data blah'
WHERE ....???

but I can't figure out what is the correct syntax?
(I've read the help files etc. but still can't figure that out.. :-(
[expired user #3565]'s profile image [expired user #3565] posted 16 years ago Permalink
Say I have a table like this:
+------+------+--------------+
| B-num| date | A-num |
| 22 | 01.08.2003 | 2 |
| 23 | 02.08.2003 | 2 |
| 24 | 03.08.2003 | 1 |
| 25 | 04.08.2003 | 4 |
| 26 | 05.03.2003 | 4 |

and want to UPDATE the values in the "A-num" column individually
or just the value of *one* field in the "A-num" column.
What would be then the correct sql query?

I found this:
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_condition]

But it tells me absolutely nothing.
I need just a real example with that WHERE command.
ansgar's profile image ansgar posted 16 years ago Permalink
Your WHERE clause normally should include the columnname=value pairs of the primary key in the table in question. I'm guessing that B-num is the column with a primary key on it because it looks like a unique auto incremented id.
UPDATE `yourtable` SET `A-num`= 'new data' WHERE B-num = 23


If 'new data' is a numeric value you can remove the single quotes around it although it also works quoted then.
[expired user #3565]'s profile image [expired user #3565] posted 16 years ago Permalink
Thank you! than helped.

BTW: I think it would be a GREAT and VERY helpful HeidiSQL feature,
if besides the "Import CSV file" button there would be a button that says
"Update from CSV".
Then the user would select the columns he wants to update the same way
as in the Import CSV file dialog
and additionally select the primary key column.

Right now I have to create a sql query for each field that I want to update.
Thanks to OpenOffice Spreadsheet Calc's quick copy function (the same as in excel - drag and copy) it can prepare those queries for each column pretty quickly and then update all fields in a column with one step.
but it would be MUCH better if I could just import the stuff for update
the same way as it works with the "Import CSV file" button
and update all fields in all columns in one single step!

I understand that there is no "UPDATE mode" in LOAD DATA
but I'm sure you could use some trick to make it possible.
For example:
Create a temporary/invisible table with new data
and then it should be as easy as pie to update from that temporary table.
Here: http://dev.mysql.com/doc/refman/5.1/en/update.html
in one of the first comments they have an example how to do that.

Please login to leave a reply, or register at first.