i need to replace only the domain of an URL present in many rows of a database with a different one. For example, the current URLs are something like www.myoldsite.com/index?s=something and i need to replace only the domain so they will look like www.mynewsite.com/index?s=something and so on. Is it even possible to do that in mysql?
I know how to update and set full values for all rows in a table having certain value but not only part of them with this:
UPDATE `mydatabase`.`mytable` SET `mycolumn` = 'http://mynewsite.com/' WHERE `mycolumn` = 'http://myoldsite.com/';
What can i do in this case?
Thank you.
Yes. Use the function replace():
UPDATE mydatabase.mytable
SET mycolumn = replace(mycolumn, 'http://myoldsite.com', 'http://mynewsite.com/')
WHERE `mycolumn` like 'http://myoldsite.com/%';
This isn't perfect, if the old site appears more than once in the URL.
Perhaps a better way is:
UPDATE mydatabase.mytable
SET mycolumn = concat('http://mynewsite.com/',
substr(mycolumn, length('http://myoldsite.com/')
)
WHERE `mycolumn` like 'http://myoldsite.com/%';
I think you're looking for something like
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%'
You should use REPLACE to replace the domain when you've found matches:
UPDATE mydatabase.mytable
SET `mycolumn` = REPLACE(mycolumn, 'http://myoldsite.com', 'http://mynewsite.com/')
WHERE `mycolumn` LIKE 'http://myoldsite.com/%'; -- wildcard on the end
The WHERE clause may be unnecessary here, but might be good for reference
you need to see -->
How can I use mySQL replace() to replace strings in multiple records?
so, you can update strings ...
UPDATE mydatabase.mytable SET mycolumn=REPLACE(mycolumn, 'old url', 'new url') where mycolumn='%old_url%'
you must be modified to fit the purpose.
Related
For example:
I have text in my column like: 'some text with word to replace' and i want to replace:
word with Word
i do:
update table set column = replace(column, 'word', 'Word');
and i get error:
Mysql: #1442 - Can't update table 'table' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
If you want to change only the first letter :
UPDATE MyTable
SET myColumn = CONCAT(UCASE(LEFT(myColumn, 1)), SUBSTRING(myColumn, 2));
If you want to change all the column :
UPDATE MyTable
SET myColumn = UPPER(myColumn);
If you want to replace some words, you have to use the replace function :
UPDATE MyTable SET myColumn = replace(myColumn, 'word', 'Word');
Please to consider to accept my answer if it's OK for you.
EDIT : Adding a third example to search and replace a word in the field and replace it to another one.
Use UPPER or LOWER functions in mysql.
I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.
Can we do it using Insert Query?
While looking for the answer I came across
UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'
So it is possible use Replace in Update query.
Can we do the same in insert query? Please Let me know if you can help me.
You can use the following:
REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)
More info from:
http://www.mysqltutorial.org/mysql-replace.aspx
You can use a BEFORE INSERT TRIGGER on your table
CREATE TRIGGER trigger_name
BEFORE INSERT
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);
I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?
To update a single column in a single table, you can use a combination of LOWER() and REPLACE():
UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))
To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:
ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name;
UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_'));
Just be sure to update the data-type in the ALTER command to reflect your actual data-type.
When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D
My Answer though:
REPLACE(string1, find_in_string, replacementValue);
I've got a classic case of UPDATE or INSERTing some data into a table. I'm not sure if I should just do an UPDATE and if i get zero ROWCOUNT, then do an INSERT. Alternatively, I've heard rumours that the MERGE statement now replaces this, but I'm not sure how and if it's appropriate, in this situation.
Here's some sample sql to help demonstrate this...
ALTER PROCEDURE [dbo].[InsertLocationName]
(
#SomeId INTEGER,
#SomeName NVARCHAR(100)
)
AS
BEGIN
SET NOCOUNT ON
UPDATE TableFoo
SET SomeName = #SomeName
WHERE SomeId = #SomeId
-- Did we update something?
IF ##ROWCOUNT <= 0
-- Nope, so add the record.
INSERT INTO TableFoo
VALUES (#SomeName)
END
thoughts?
Sure - the MERGE syntax is probably the easiest. You basically need:
a target table to update
a source table to read from
a JOIN condition
a bunch of statement to execute for matched or non-matched rows.
So it basically looks something like this:
MERGE TableFoo as t
USING TableFooSource as s
ON t.SomeID = s.SomeID
WHEN MATCHED THEN
UPDATE SET t.SomeName = s.SomeName
WHEN NOT MATCHED THEN
INSERT(SomeName) VALUES(s.SomeName)
;
Don't forget the semicolon at the end!!
Marc
PS: Updated to use your table and field names. The point here is - the set of data used to be updated needs to be in a source table of its own (if needed, bulk-import that from e.g. an external file) and then the whole operation (all INSERTs and UPDATEs) are done in a single SQL statement.
Bit more of an explanation here: http://www.databasejournal.com/features/mssql/article.php/3739131/UPSERT-Functionality-in-SQL-Server-2008.htm