I have the following issue. I have a table called titles with the following structure:
id int(10),
name varchar(100),
At some point later we added a new column called modified_name. It is defined as the same as name except that it is lower case and has all of the spaces replaced with a -. We added this column and so we needed to now get the right modified name value into each record of that column. To do this we wrote a PHP script that handled that by loading in values from the database and processing them, but that is highly inefficient. Is it possible to write a single UPDATE query that would add the correct value to each record in the titles table. I can think of ways to do this with a stored procedure and a while loop there in, but I want to know if something more efficient is possible. It there any way to achieve something like the following:
UPDATE `titles`
SET
`modified_name` = LOWER(REPLACE(SELECT `name` FROM `titles` WHERE id = PRESENT_VALUE), ' ', '-');
The goal being to SET the modified_title column of every record in the titles table to a unique value that results from that record's name column as followed:
# Before modification update query
name = "Hello Goodbye"
modified_name = ""
# After modification update
name = "Hello Goodbye"
modified_name = "hello-goodbye"
Thank you for your help, any advice on how best to do this would be appreciated.
UPDATE `titles` SET `modified_name` = LOWER(REPLACE(`name`, ' ', '-'))
Related
I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)
I am getting data piled up in only one column and i want to separate the information
This is what im doing:
UPDATE `Inventario`.`inventario`
SET `DESCRIPCION`='MONITOR 17\",,DELL '
WHERE `ID`='473';
for example DELL should be in another column
You can set multiple columns in an UPDATE statement:
UPDATE `Inventario`.`inventario`
SET `DESCRIPCION`='MONITOR 17"',
`Manufacturer` = 'DELL'
WHERE `ID`='473';
BTW, you don't need to escape " inside a string delimited with '.
UPDATE Inventario.inventario SET DESCRIPCION='MONITOR 17\"', Manufacturer = 'DELL' WHERE ID='473';
If your table has the column with the name manufacturer.
I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?
I need to update one column if another column has a specific data.
Usually if I want to update one column, I do the following SQL Query:
UPDATE table1
SET field1 = replace(field1, 'oldstring', 'newstring')
But what I can't figure is how to make it look up one column, and if that field has some data, it should update another column's field.
Here is what I want to do.
look in table: phpbb_tree
under column: spouses_total
if the field is empty (has no data)
update column: page_template
update from: tree_body_spouse_1.html to: tree_body_single.html
So basically, I know how to do the "update" part, but don't know how to make it look first in one column, and if empty (or matches) it should do the following:
UPDATE phpbb_tree
SET page_template = replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
Hopefully someone could tell me how to write it up. I don't even know if it's even possible to do a search for an empty data in a column?
You could use CASE expression to fulfill different condition of replacement.
UPDATE phpbb_tree
SET page_template = (CASE
WHEN spouses_total is null
THEN replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
ELSE page_template
END
);
Edit:
Please check this..
SQL Fiddle HERE
I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.