How to update multiple column values in MySQL table - mysql

I want to update image path in MySQL table. Table schema is this:
table_brands - brand_id, brand_name, brand_image_path
Currently path is stored as-
`images/1.png`
`images/2.png`
I want to make it as
`images/brands/1.png`
`images/brands/2.png`
Also for some entries path is stored as
`images/brands/1.png`
`images/brands/2.png`
so changes should not be done for such entries.
Can someone help me?

You can use replace function for this purpose only for the image paths that don't contain 'brand'
UPDATE table_brand
SET brand_image_path = REPLACE(brand_image_path, 'images', 'images/brands')
WHERE brand_image_path NOT LIKE '%brands%';

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;

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.

MySQL merge data from related tables and export as .csv

I don't know MySQL very well so I need a bit of help with this task.. I hope it's ok to ask.
I have a table that i need to export which i did using the following code:
SELECT *
FROM `members`
WHERE `active` =1
AND `level` = 'full'
LIMIT 0 , 30000
This works perfect and gives me most of what I need including a user ID in the collumn called id but I also need to grab the related image for each member and add it to my exported .csv.
The images (filename) are stored in a different table called images but this only contains galleryid, filename and defaultimg (note I am only interested in data where defaultimg = 1)
The issue here is there is NO reference to the member (id) in this table.
For that I have to reference a different table called: imagelinks which contains 2 columns:
1) id which relates to my galleryid from the images table.
and
2) memberid which relates to the user ID id from the members table.
What a disaster of a setup I know.
So.. what I need to do and can't for the life of me work out is export a .csv with ALL the user data from the members table (as stated in my code above) with the addition of the filename data from the images table that coresponds to the referenced user (where defaultimg = 1).
Lastly, I also need to prepend a URL to the front of the filename data.
eg: The table has my-image.jpg but in the exported .csv I need it to be http://www.domain.com/images/my-image.jpg
I'm trying to do all of this in PhpMyAdmin.
Any help with this will be very much appreciated.
Thanks
My syntax might be a bit off but this should be close...
SELECT members.*, CONCAT('http://www.domain.com/images/', images.filename) AS thefilename FROM members INNER JOIN imagelinks ON members.id=imagelinks.memberid INNER JOIN images ON images.galleryid=imagelinks.id WHERE members.active=1 AND members.level='full' AND images.defaultimg=1

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);