How to insert derived attribute in mysql - mysql

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;

Related

Mysql insert from multi source

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

Insert data in mysql from multiple sources

I want to insert values from different sources. For example
insert into a (('id','name','add'),'college')
select from b where id = 1,'abc'
Here there is no timestamp field in table b
I would rewrite your query as follows:
insert into a (id, name, add, college)
select id, name, add, 'abc' from b where id = 1
With this query, the 4th column in a will be assigned a constant value of 'abc'.

SQL: How to insert data in specific column with specific id as well

Is it possible to perform kind of that query on single table?
I have read that it could be done by multiple tables (inserting in first table by selecting that specific value from second )
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';
Is it possible to perform kind of that query on single table?
Yes, it is possible:
CREATE TABLE Customers(ID INT IDENTITY(1,1),
CustomerName VARCHAR(100), Country VARCHAR(100));
INSERT INTO Customers (CustomerName, Country)
VALUES ('John', 'USA'), ('Martin','Germany');
INSERT INTO Customers (CustomerName, Country)
SELECT CustomerName, Country
FROM Customers
WHERE Country='Germany';
SELECT *
FROM Customers;
LiveDemo
Keep in mind that Table Spool is required to avoid Halloween Effect

mysql split data from one table into two tables with mapping(association) preserved

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.

With MySQL, how do I insert into a table on condition that the value does not exist in same table?

insert into student (FirstName,AGE,CITYID) values('guna','26','1')
select * from student WHERE FirstName!='guna';
This query showing error.I can't make FirstName column as unique. Please give an idea other than this.
Thanks
INSERT INTO student ( ....)
WHERE FirstName NOT IN (SELECT FirstName FROM student)
After revision and testing:
INSERT INTO student
(FirstName, age, cityid)
SELECT
'guna','26','1'
FROM student -- any table name will do
WHERE 'guna' NOT IN
(
SELECT FirstName
FROM student
)
LIMIT 1 -- required because all rows will qualify if
-- WHERE clause is satisfied
You can add a unique index on that table which will do the same for you
ALTER TABLE student ADD UNIQUE <name_of_index>[optional] (FirstName);
EDIT:
If you cant use a unique index..
One soln i can think of is using compound statements - http://dev.mysql.com/doc/refman/5.0/en/if-statement.html