Inserting Data into a Column within the same table - mysql

I would like to Populate the username field/column with the first letter of the survey_responders firstname concatenated with the last name of the survey responder.
Every time I run my code it gives me an error :
You can't specify target table 'survey_responders' for update in FROM clause"
Any help with what I'm doing wrong anyone?
update survey_responders
set username = ((select CONCAT(left(first_name,1), last_name)
from survey_responders)
);

try this:
UPDATE survey_responders SET username = CONCAT(LEFT(first_name,1), last_name);
You don't need to do the SELECT query that you did. In UPDATE command, in every row you can use all the columns that you have.

Related

MySQL Query: UPDATE and/or APPEND

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)
)

Query to update particular column field in SQL

I tried the following queries to change the column field of the table 'role' from 'Admin' to 'Administrator'. I am not informed about the values of id. my table 'role' contains two columns 'name' and 'id'.
None of the queries worked. Where am I going wrong?
update table role set name=replace('Administrator','Admin');
update table role set name='Administrator' where name='Admin';
select replace('Admin','Administrator') from role;
You don't use a table keyword so the second example is almost correct
update role set name='Administrator' where name='Admin';
The second would be the standard way:
update role -- "table" is not appropriate
set name = 'Administrator'
where name = 'Admin';
My guess is that you have no row that matches the condition -- presumably because of bad/hidden characters in name. Try this:
select *
from role
where name = 'Admin';
If this returns no rows, then try like:
where name like 'Admin%'
where name like '%Admin'
where name like '%Admin%'
Or maybe, using regexp:
where name regexp '[[:<:]]admin[[:>:]]'
(note that regexp is, by default, not case sensitive)
If one of these matches the row you want, then use it in the update.
you need to alter the table structure so the Mysql comand should be:
ALTER TABLE role CHANGE Admin Administrator [Admin column data type]

how to use select along with insert query in MySQL?

Hi I'm a rookie to Mysql. I have table called course(c_id,c_name). I have inserted c_id and left c_name blank. Now how can I enter the c_name for that particular c_id using select statement inside the insert query ?
Use a simple update query, since you've already inserted the row. E.g. (replace x with your id):
update course set c_name = "your value" where c_id = x

SQL Error when trying to insert one value into one row for a column

I'm using MySQL 5.7 and for some reason my INSERT statement isn't working as before even though the syntax looks correct. It's error-ing out on the where statement...
SQL:
insert into users(age) values('16') where username='r';
If the row for username r already exists perhaps you are looking to update the age value instead?
Use: update users set age = 16 where username = 'r' instead.
Also, I'm just guessing here, but maybe age holds a numeric value, and if so you can remove the quotes around 16.
That syntax isn't correct. You can't use where like that. Perhaps you want something like:
insert into users (username, age)
values ('r', '16')
http://dev.mysql.com/doc/refman/5.7/en/insert.html
Alternatively if that user already exists, you might be looking for an update statement instead:
update users set age = '16' where username = 'r'
INSERT statements must not contain a WHERE clause. Remove it.
If, in fact what you are trying to do is update an existing row, use an UPDATE statement, not an INSERT statement.
update users set age = '16' where username='r';
If you want to insert new records in your table, you have to write query for inserting data.
SQL INSERT INTO Statement syntax is this:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
In your case, if you don't have the record in your database, your query will look like this:
INSERT INTO users (username, age)
VALUES ('r', '16')
But if you want to update existing records in your table, you have to write query for updating data using the SQL UPDATE Statement.
The syntax for this is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
To update the record/s, you have to specify clause in WHERE which record should be modified.
To modify age of user/s with username that is equals 'r', this is the query:
UPDATE users SET age = 16 WHERE username = 'r'
but if you want to modify for all users which usernames starts with 'r':
UPDATE users SET age = 16 WHERE username = 'r%'
I hope this explanation will help you to understand better SQL statements for INSERT new and UPDATE existing records.

Mysql update rows dynamically based on column input

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`, ' ', '-'))