I have a MySQL table named "users" that has the columns "firstname" and "surname". There are a thousand or so rows in the table.
I have now added another column named "search" and I would like to populate it with the values of both the first name and the surname separated by a space. For example, if firstname = Bob and surname = smith, i would like to populate "search" with "Bob Smith".
Can anyone advise on an update statement that selects these values and inserts them in to the new column?
Best regards, Ben.
You could simply use...
UPDATE users SET search=TRIM(CONCAT(firstname, ' ', surname));
As an explanation, CONCAT simply concatenates (merges) the supplied fields/values, and TRIM will remove any leading or trailing spaces, hence ensuring that there are no issues if a firstname/lastname or indeed both are missing. See the MySQL String Functions manual page for more information on these functions.
However, I'd be tempted to call the new column "name" (or indeed "full_name") rather than "search", as this is, at best, a somewhat misleading field name.
update users
set search = CONCAT(firstname, ' ', surname)
This script will update it.
I'm not sure about MySql, but Sql Server lets you set a field as a calculated column. You can specify the calculation as "ISNULL(FirstName, ' ') + ' ' + ISNULL(Surname, ' ')" and set it as persisted so it doesn't calculate it each time. In that case, you wouldn't need an update script
update `users` set `search` = CONCAT(`firstname`, ' ', `surname`);
Related
I wanted to create a query in MYSQL that would trim the leading '# ' from all of the fields in a column.
update values set value = TRIM(LEADING '# ' from value)
However, doing this gives me an error
Duplicate entry '3002' for key value
The value column has a unique constraint and the error probably occurs because the query is trying to set the same value to all of value column after trimming.
Is there a way to do trim leading and update in mysql?
This query looks fine.
The issue might be here. Let me give you an example.
Case 1:
At row-x, you have value '#info' and at row-y you have info.
You removed the # from row-x in your query. Now you have already info value at row-y. You can not update the new value to info as it is there already.
I would suggest either to remove the UNIQUE constraint or you do not update the database itself. You can trim with your backend programming.
This is not setting all values to the same value, this is where once trimmed you now have two different rows with the same value for that column.
To find this:
SELECT id, value, TRIM(LEADING '# ' from value) AS trimmed_value ORDER BY trimmed_value
Presuming you have some kind of id column you'll be able to find any rows where trimmed_value is identical.
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 have a large (1 million + row) database/table that includes a "Name" field. However, I need a firstname and lastname field.
I am able to run queries to display the fist and last names the way I need them in the new fields (parsing), but I can't seem to get those query results to INSERT into the table.
I was able to create the fields in the table, but not populate them.
I am working in phpMyAdmin, and the table was exported from Access via ODBC.
A couple examples of code that doesn't work are below. It's been a few years since I had to work with SQL and I think maybe the logic of this approach is just wrong. I certainly appreciate any help.
Example that doesn't work:
INSERT INTO fed2012_aquabarndesign_com (lastname)
select left(Name,InStr(Name,',')) AS lastname
from fed2012_aquabarndesign_com
or
INSERT INTO fed2012_aquabarndesign_com (lastname)
Values (select left(Name,InStr(Name,',')) from fed2012_aquabarndesign_com);
Here's how you can do it with an UPDATE -- not sure you want to INSERT those records back to the same table:
update fed2012_aquabarndesign_com
set firstname = trim(left(name, instr(name, ' ' ))),
lastname = trim(right(name, length(name) - instr(name, ' ' )))
SQL Fiddle Demo
This does assume each full name has a space in between the first name and last name fields.
If you really want to reinsert those rows, then this should work:
insert into fed2012_aquabarndesign_com (firstname, lastname)
select trim(left(name, instr(name, ' ' ))),
trim(right(name, length(name) - instr(name, ' ' )))
from fed2012_aquabarndesign_com;
SQL Fiddle Demo
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`, ' ', '-'))
Say, you got a table of 100 records. And field age contains a some integers. And you want all those integers to be incremented by 1.
or you got a textfield called name and a bunch of names in there. And you want all those names to be prefixed as Mr..
Is there a way to achieve this in one SQL command?
The alternative would be to compile a recordset of these 100 recs and going thru a loop and then running an individual update statement.
Use the update command
update yourtable
set age=age +1
update yourtable
set name = 'Mr. ' + name
where gender='M'
UPDATE mytable SET age = age+1
UPDATE mytable SET name = CONCAT('Mr. ', name)
If MySQL is in ANSI mode – specifically, PIPES_AS_CONCAT, you can use 'Mr. ' || name instead.