I am trying to replace a string like "example.com" with "newdomain.com" in a MySQL table called "wp_options" with a column name "option_value".
I have seen a lot of other questions about replacement but the problem is that they are all using the REPLACE function, which damages the structure of my table because it removes the row (if matches) and inserts a new one(1), which makes the primary key and unique id disappears (for example, this is causing me to lose my theme configuration).
Is there a way to replace such string using the INSERT function? or the UPDATE?
A simple update statement should do it:
UPDATE wp_options
SET option_value = 'newdomain.com'
WHERE option_value = 'example.com'
EDIT:
If the requirement is to search within the value as clarified by the comment below, you can use the replace function:
UPDATE wp_options
SET option_value = REPLACE(option_value, 'example.com', 'newdomain.com')
WHERE option_value LIKE '%example.com%'
Related
I find it difficult to fill a new column that I just added to an already existing table in SQL
I tried the Insert command, to fill in the table afresh, but couldn't see it true because the table has 1445 rows
You could specify a default value when you add the column:
ALTER TALE mytable ADD COLUMN new_column VARCHAR(10) DEFAULT 'my_value'
Alternatively, once you added the column, you could use an update statement:
UPDATE mytable
SET new_column = 'my_value'
Just like the answer above you can use ALTER TABLE to add a new column
ALTER TABLE dummy
ADD temp_column VARCHAR(50);
Then you can use the update and add a where statement to be more specific
UPDATE dummy
-> SET temp_column = 'DEFAULT'
-> where id > 10;
Alternatively you could try using Five, you can import your database in it as an SQL dump, it actually allows you to do way more with new columns, like copy data from an existing field in the table or fill in the values from a query
here is an image of Five prompting you to fill in values for a new column
there are other things as well like you can write queries directly in Five, reuse the queries and the tables can be generated with simple point and click.
Disclaimer: I work for Five
I've added a new column to the ls_customer table that goes by response_category. I then set an update code to it.
ADD statement:
ALTER TABLE ls_customer
ADD response_category VARCHAR(255)
UPDATE Statement:
UPDATE ls_customer SET response_category = 'Hot Lead' WHERE flag = 3
UPDATE ls_customer SET response_category = 'Warm Lead' WHERE flag = 2
How do I set the above "UPDATE" statement to auto update whenever the flags change in our internal CRM tool?
I then use a SELECT statement to present this data. The problem is this "SELECT" statement is linked to my Tableau analytics. Everytime I refresh Tableau, if there's any changes in the flag, the response category doesn't get updated in the final SELECT statement untill I manually open MySQL and run the above "UPDATE" query again.
The SELECT statement is as follows:
SELECT
cell_phone,full_name,company_name,DATE(date_join),
response_type,response_category,ls_customer.tags AS lead_type,ls_lead_stats_raw_data.last_update,date_join,source
FROM ls_lead_stats_raw_data
INNER JOIN ls_customer
ON ls_lead_stats_raw_data.client_id = ls_customer.customer_id
INNER JOIN ls_company
ON ls_company.company_id = ls_customer.company_id
How do I automate the UPDATE statement to set response_category to 'Hot Lead', 'Warm Lead' everytime I change the flags in our CRM?
Despite mysql version number, there are many different ways you can solve this issue, such as:
creating a before update trigger on your table updating response_category column based on flag column content. But, maybe your CRM already use triggers and will not allow you to add any code to them
creating an database event executed every minute and updating the response_category column. this solution is not the most effective as it will execute the event, even nothing has changed in the flag column.
you can create a reference table with the flag and the response_category columns. It's content would be :
flag
response_category
2
warm lead
3
hot lead
You add this new table with a join on ref_table.flag = ls_customer.flag to your select (replace response_category by ref_table.response_category). But this solution is so obvious, that I think there is a good reason you didn't use it.
So finally I would recommend to use a generated column instead of a regular column for response_category:
ALTER TABLE ls_customer
ADD COLUMN response_category VARCHAR(255) GENERATED ALWAYS
AS (CASE flag WHEN 2 THEN 'Hot lead' WHEN 3 THEN 'Warm lead' END)
STORED ;
Using STORED option will allow you to add an index on this column in case you need to improve your SELECT performance.
Hope this will help.
I need replace string1 to string2 in column big table. My table weight is1.7GB. For update I will use:
UPDATE table
SET column = REPLACE( column, 'search', 'replace' );
So, I have several questions:
Will table lock during procedure? Should I hide the public part of my site?
How long will this procedure be executing?
How better to run this? Maybe 'screen'? if my ssh connection will closed.
MySQL 5.5.53
You shouldn't run replace on all the rows of the table.
Instead add a where condition as #reds said in the comments.
This will make your update much more efficient and is the correct way to do it.
UPDATE table SET column = 'replace' where column = 'search';
In my case:
UPDATE table SET column=REPLACE(column, 'search', 'replace') WHERE column LIKE "%search%";
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
UPDATE items SET name = 'haha' WHERE id = '12'
I'm curious if update also inserts the values if the where condition fails. I've read on w3schools that update only updates existing data on the database but on my script it's automatically inserting rows with the data. I am wondering if it might be a bug in the script or that's just how UPDATE works on mysql.
No. If, in your example, there's no entry with id = 12 in the database, the query will return "no rows affected". An update will never create a new entry in MySQL.
EDIT: although update won't create a new entry, it may include default/automatic values set up in your database schema (current timestamp, for instance).
NO. Update does not insert a value if the value doesn't exist in table. Please check if the script checks if the status of the update and makes another call to DB to insert the data.
Your SQL should do the following -
Update all records in the items table that have an id of 12 by setting their name to 'haha'
Update won't insert records if they don't exist, it will only update existing records in the table.
Short answer: No.
Long Answer: If your column doesn't exist you will get an error. If your where condition column doesn't exist you get error too. If your where condition value doesn't exist, it do nothing.
I use a temp table to review the data update conditions, you can refer
UPDATE table1
SET
column1 = 'things'
WHERE
IDcolumn = 'id' AND
(NOT EXISTS (SELECT * FROM (SELECT * FROM table1) AS temp WHERE temp.column1 = N'things'))