how to delete single record from list - mysql

I have a record in table column like this (1001,1002,1003,1004,1005)
and I want to delete "1003" from this list. Please help me.

For your own good, don't store data like this. This type of issue is not, by far, the biggest problem you will run into.
This being said, you can solve you issue by using:
UPDATE TABLE
SET COLUMN = REPLACE (COLUMN, ',1003,', ',')
WHERE ID = PK;

even this will work:
update tablename set column=(select
substr(column,1,instr(colname,',',2))||susbtr(column,instr(column,',',3),length(column)-
instr(column,',',3)) from tablename where id=value;

Related

Moving data from one mySQL Table field to another Table field where each table has a matching id

I need to move all the data from table FCSTMR field CSTYPE to table customers2 field customers_group_pricing.
The value stored in FCSTMR CS_ENCSUNIQUE matches that stored in customers2 customers_id.
I tried using the following mysql, but it didn't move any data at all.
UPDATE customers2
SET `customers_group_pricing` = (
SELECT `CSTYPE`
FROM FCSTMR
WHERE CS_ENCSUNIQUE = customers2.customers_id);
Where did i go wrong with this? I'm assuming something to do with the WHERE statement.
Have a look this question's last comment. I think it is similar to yours.

MySql Add onto an exisiting entry in the database

Just wondering is it possible to add onto an exisiting entry in the database without having to do a query to look whats in there?
Ive done it with numbers before like
UPDATE table SET views = views +1
But is there a way to do it with a string?
Thanks
UPDATE table SET views = CONCAT(views,'some text to add on to it')
What you are looking for is CONCAT() function:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
UPDATE table SET field = CONCAT(field, "some str" ) WHERE id = 123;

Changing column content in many rows

I have a column called code and in every row, the column contains FE. Because I do not want to go through 13,000 records, is there a quick way to replace FE inside 'code' with FEU?
While I appreciate this may be a simple question (or not?), I wasn't sure how to word it in order to find a solution.
You should be able to do like this:
UPDATE <table> SET code = REPLACE(code, 'FE', 'FEU');
If the column really just contains the value FE a simple WHERE clause should be enough:
UPDATE <table> SET code = 'FEU' WHERE code = 'FE';
Maybe something like this?
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'
This will work if your column contains only the string 'FE' and you want to replace it with 'FEU'.
update `table`
set `code` = replace(`code` , "FE","FEU")
where (if there is a where write it here)
should do you
This is probably the quickest way. You can try this:-
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.

add the same thing to a table column by sql command

there is a table named test. in it. there are some columns as this:
001.jpg
...
999.jpg
now i want to use a sql command to add a url before them. as this http://www.example.com/xxx/001.jpg.....is there a way to get this? thank you.
There is two way to accomplish this task. It depends up to you that what you want?
If you want to add a url in the database permanently then you have to use update query with no any where condition, Although if you want to only show your field with this added url you have to use select query.
Please find below the examples for both:
Suppose that your table column name is imageName then UPDATE query will be
UPDATE test SET imageName = CONCAT("http://www.example.com/xxx/", imageName);
And the SELECT query will be
SELECT CONCAT("http://www.example.com/xxx/", imageName) FROM test;
Supposing that your field is called url, a simple UPDATE query will do:
UPDATE test SET url = CONCAT("http://", url);