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;
Related
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
I need some help again.
I have to insert two values in a table into "hospital_database". This table has five columns, and it's called "personas". The columns' names are "cod_hospital(PK)", "DNI", "Apellidos", "Funcion" and "Salario"... I have to insert "99887766" and "Martínez Martínez, Alejandro" into "DNI" and "Apellidos", but according to the question, I must to insert into a "hospital" where only there's 1 person...
I have to use "insert+select" and my last effort was this:
insert into personas
values (99887766, 'Martínez Martínez, Alejandro');
select dni, apellidos
from personas
where count(dni)=1;
I tried something like that, and a lot of ways... but It doesn't work like the question asks. I have to use insert+select, so I shouldn't write ";" before "select".
Honestly I'm still guessing at this a little, but maybe you intend to insert an additional row in the personas table if only 1 row exists in that table for a given hospital code. To do this you need to use group by with having:
insert into personas (cod_hospital, dni, apellidos)
select cod_hospital, 99887766, 'Martínez Martínez, Alejandro'
from personas
group by cod_hospital
having count(*)=1
insert have several sintaxis and looks like you are mixing all of them
One is insert values http://www.w3schools.com/sql/sql_insert.asp
INSERT INTO table_name
VALUES (value1,value2,value3,...);
the other insert from a select
http://www.w3schools.com/sql/sql_insert_into_select.asp
INSERT INTO table2
SELECT * FROM table1;
And if you dont include all the field, you have to indicate which fields are you inserting
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
cod_hospital is the table's PK, you could only insert one person per hospital, change your PK to: cod_hospital, dni
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.
Lets say I have a table called Students with columns Name, mobile, email, preferred course, preferred school, gender, age, address, qualification, designation, homephone, workphone
I want to select the data from the Students table and insert into 2 tables such as,
Inquiries => (with columns) Id, Name, Mobile, Email
Enrollments => (with columns) Id, inquiry_id, name, mobile, email, preferred course, preferred school, gender, age, address, qualification, designation, homephone, workphone
How to populate the inquiry_id in the enrollments table with the correct id value from the inquiry table?
Use LAST_INSERT_ID() to get the id of the last inserted inquiry;
INSERT INTO inquiries (name, mobile, ...
INSERT INTO enrollments (inquiry_id, name, ...)
VALUES (LAST_INSERT_ID(), 'myname', ...
A simple SQLfiddle for testing.
Another approach for bulk moving is to add a temporary column to hold the old id so that you can use the correct one for the second insert;
ALTER TABLE inquiries ADD COLUMN oldid INT;
INSERT INTO inquiries (..., oldid) SELECT ..., id FROM students;
INSERT INTO enrollments (inquiry_id, ...)
SELECT (SELECT id FROM inquiries WHERE students.id=oldid), ...
FROM students;
ALTER TABLE inquiries DROP COLUMN oldid;
Another SQLfiddle.
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;