update all rows with adding text before it - mysql

Below is how my table is
create table tab (id INT, fullname varchar(100));
Data is
insert into tab values
(1,'Full Name 1'),
(2,'Full Name 2'),
(3,'Full Name 3'),
(4,'Full Name 4'),
(5,'Full Name 5'),
(6,'Full Name 6');
I want to update the table with fullname as My Full Name is + actuallfullname. e.g. data for id 1 should be My Full Name is Full Name 1.
Any idea how to get this done in one query?
Using below query, it would be executing n times as I have so many records.
UPDATE tab SET fullname='My Full Name is Full Name 1';
sqlfiddle

Use CONCAT.
UPDATE tab
SET fullname = CONCAT('My Full Name is ', fullname)
SQLFiddle Demo

You can use MySQL's CONCAT() function:
UPDATE tab SET fullname = CONCAT('My Full Name is ', fullname);
But does this really belong in the database? Sounds like something one would normally perform at the presentation layer of one's application.

Ref CONCAT
UPDATE tab SET fullname=CONCAT('My Full Name is ',fullname);

UPDATE tab SET fullname = CONCAT('My Full Name is ' , fullname)

Related

SQL - Want to output the ones with the same first name but different last name. Full name in the same column

How do i go on about on a query for where i wanna extract the same first name but the last name is different?
NAME
------
Chris Stutter
Chris Lamb
Alfred Dark
Kristine Light
output:
Chris Stutter
Chris Lamb
I've made a script for you for this specific condition. Based on the info you shared, I've created the test scripts below. You can test this script and see the result really quickly at https://onecompiler.com/mysql/
-- create
CREATE TABLE YOUR_TABLE (
Name TEXT NOT NULL
);
-- insert
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Lamb');
INSERT INTO YOUR_TABLE VALUES ('Alfred Dark');
INSERT INTO YOUR_TABLE VALUES ('Alfred Dark');
INSERT INTO YOUR_TABLE VALUES ('Kristine Light');
-- fetch
SELECT DISTINCT *
FROM YOUR_TABLE
WHERE SUBSTRING_INDEX(Name, ' ', 1) IN (
SELECT *
FROM (
SELECT SUBSTRING_INDEX(Name, ' ', 1) as firstName
FROM YOUR_TABLE
GROUP BY firstName
HAVING COUNT(DISTINCT Name) > 1
) AS DuplicatedFirstNames
);
You should be able to utilize this fetch script as a reference and modify it accordingly for your own purpose now.
You want to work with first name and last name in your database, but your table doesn't provide that information. It only has a column for the full name. This means that your database design is not appropriate for the task. The information you seek is not stored atomic, but in a concatenated form and thus violates the first normal form. (This also shows that database normalization sometimes depends on how you want to work with the data.) The best way to deal with this problem is hence to change your database and make first and last name separate columns.
If you cannot change the database design, the first task is to find out in which formats the names are stored. So far you have shown "first name - one blank - last name". If this is the only format, then it is rather easy to split the two. If, however, you also have to deal with 'Smith, John' or 'Arthur Conan Doyle', it gets more complex. Let's say all names are in the same format. So, split first name and last name and work with these.
Once you have separate first and last name, the task becomes easy. You are looking for names for which exists another last name with the same first name, i.e. use EXISTS.
WITH names AS
(
SELECT
SUBSTRING_INDEX(name, ' ', 1) AS first_name,
SUBSTRING_INDEX(name, ' ', -1) AS last_name
FROM mytable
)
SELECT first_name, last_name
FROM names
WHERE EXISTS
(
SELECT NULL
FROM names other
WHERE other.first_name = names.first_name
AND other.last_name <> names.last_name
)
ORDER BY first_name, last_name;

Using INSERT with condition

there is a table customers with three columns (Id, name, Phone).
For example there is charachter with id 1 and name is Tom, and there are a lot of differents id and names so i need to add a phone number solely for tom in this sutiation, how can i do it?
i tried this:
INSERT INTO Customers (Phone) values ('a Number') WHERE Id = 1;
I can get that i use condition "where" wrong, how should i use it right in my situation, please help, and thanks.
It seems that you actually want an update here:
UPDATE Customers
SET Phone = 'a Number'
WHERE Id = 1;
If you really do want to add a new record, then drop the WHERE clause:
INSERT INTO Customers (Phone) VALUES ('a Number');
If the Id column be auto increment, then MySQL will auto generate a value for the Id.
In the described case (update of already existing record) you should use UPDATE statement instead of INSERT:
UPDATE Customers SET Phone = "customer_phone_number_here" WHERE Id = 1;
Hope, this link would be useful as well.

How to insert into a table from another table and from user input?

What I am trying to do is something like this
INSERT INTO BOOKS(borrower_name,isbn) VALUES("Jane",SELECT isbn from TABLE...);
This is wrong obviously. But I am trying to get something that would work the way this would (if it did).
You were very close
INSERT INTO books (borrower_name, isbn)
SELECT 'Jane', isbn
FROM table_name
-- WHERE id = ?
You might want to limit number of rows coming from table_name by using WHERE clause and proper condition(s)
Here is SQLFiddle demo
More over if you meant to return from SELECT only a scalar value (just one ISBN) then you were correct in the first place and could've used syntax showed in your question
INSERT INTO books (borrower_name,isbn)
VALUES ('Jane',
(
SELECT isbn
FROM table_name
WHERE id = 1
));
Here is SQLFiddle demo
What you were doing is also right, u only missed () for select statement:
INSERT INTO BOOKS(borrower_name,isbn) VALUES("Jane",SELECT isbn from TABLE...);
should be modified to:
INSERT INTO BOOKS(borrower_name,isbn) VALUES("Jane",(SELECT isbn from TABLE...));
and it will work perfectly fine!! here is the SQLFiddle demo
INSERT INTO BOOKS(borrower_name,isbn)
SELECT 'Jane', isbn from TABLE
Check This My Answer...
Example:
I have two table Employee and Client
Insert into Employee (name, city) values((select name from client where id=2),'example');
follow this method

MySQL Populate a new field with query results from another field in the same table

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

Mysql append data to fields?

I have a table which contains 3 columns, fname Lname and description. I want to take the values of Fname and Lname and append them to description (Move this data to the end of the description field without overwriting the current info in the field). Is this possible with MySQL at all?
Please update function as per req...and concatination of string :
Here Concat function is used for concat string..:
update table_name set description = CONCAT(description,'
',fname,' ',Lname);
update your_table_name set description = CONCAT(description,' ', fname,' ',Lname);