I need to insert into table with 3 columns, 2 columns from a select and one column external.
Like this:
Insert into customer(username, fullname, rate)
values
((select username, fullname from users), 1500)
It return : column count doesnt match
You want the insert ... select syntax, with a literal value in the third column:
Insert into customer(username, fullname, rate)
select username, fullname, 1500 from users
Related
I have problem with probably a simple matter:
I want to insert data from one table into another.
TO THIS table :
(ID_autor, name, surname)
FROM THIS :
autor(ID_autor, name, surname, nationality, birthday)
This is my code :
INSERT INTO tab1 (ID_autor, name, forname)
SELECT ID_autor, name, forname, nationality, birthday
FROM autor;
But I dont know why it is bad ?????
You should only select the columns that have corresponding columns in the destination table. Since there's no nationality or birthday columns in tab1, leave those out of the select list.
INSERT INTO tab1 (ID_autor, name, forname)
SELECT ID_autor, name, forname
FROM autor;
INSERT INTO class
(name, description, personid)
Select name, description, 12 from Class where PersonID = 3;
Select * from Class
Select * from Person
Why is the values words is missing from above statement? I thought it should be like this insert into tableA('name') values('select name from tableB') ?
INSERT INTO my_table VALUES ()
Insert data one Table to another table
OR
Not Using Value keyword
Insert into Table2 (Name , Address , Mobile) Select Column 1, Column 2 , Column 3 From Table1
There are different techniques of INSERT, the code above is inserting values from the table itself and change only the personid to 12, he use select so that he can copy the data aside from hardcoded personid . that's why you didn't see the VALUES keyword , but that's true.. the basic insert statement we learn from school is INSERT INTO TableName (Col1, Col2... etc) VALUES (Value1, Value2... etc) , INSERTION of data depends on the requirements that you are working on.
INSERT INTO numbers (type, number)
VALUES 'telephone', SELECT DISTINCT tel FROM flat_data
I am attempting to select distinct numbers from one table and then insert them into another table.
However I need to manually set the type column that I am inserting into manually.
I can do it without the DISTINCT but I can't get my head around how to do it with!
INSERT INTO numbers (type, number)
SELECT DISTINCT 'telephone', tel
FROM flat_data
I have two variables
1. inserted_user_id,
2. inserted_address_id
in my MySQL procedure.
I need to use them in insert queries and select queries. Im trying something like
insert into user_new(name, company, email, customer_id) select name,
company, email, inserted_user_id from user_old;
insert into user_address_map(address_id, user_id) select
inserted_user_id,inserted_address_id ;
There two statements are not working. How to use procedure variable's value in the above sql statements?
First make a select to get all the info: (you need to declare these variables first)
SELECT name,
company,
email,
INTO varname, varcompany, varemail
FROM user_old
then use it into insert
INSERT INTO user_new
(name,
company,
email,
customer_id)
VALUES (varname,
varcompany,
varemail,
inserted_user_id);
edit:
First insert the Selected values and get the id of the inserted row
INSERT INTO user_new
(name,
company,
email)
SELECT name,
company,
email
FROM user_old
SET out_param = last_insert_id();
Then update this row with your param
UPDATE user_new
SET customer_id = inserted_user_id
WHERE id = out_param;
i have two tables 1 table is student table with (sid,dob,address) another table is details where the attributes are (sid, age, marks) now while inserting into details table i want to get the age derived automatically from student table (dob) without entering how can i do this
You can insert the result of a select:
insert into details(sid, age)
select sid, datediff(current_date, dob)
from student
where sid = 123
You can get the age as follows:
select to_days(now())/365-to_days('1991-08-21')/365;
or
select period_diff(date_format(now(),'%Y%m'),199108)/12;