Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

INSERT INTO with subquery and text value at once

enter image description here

I want to put data to employees table by inserting data in this way:

insert into employees(lastName, firstName, extension, email, officeCode, jobTitle)
    select contactLastName, contactFirstName 
    from customers 
    where country like 'usa'
    union all
    select officeCode 
    from offices 
    where country like 'USA' 
    limit 1;

But extension, email and JobTitle doesn’t exist in customers and offices.
Is there any option to make INSERT INTO with subquery and text values for those 3 columns?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Should be able exclude the columns you don’t want as long as they aren’t required fields and then you can join to your offices table.

INSERT INTO employees (lastName, firstName, officeCode, extension, email, jobTitle)
SELECT contactLastName, contactFirstName, officeCode, ‘999’, ‘email@email.com’, ‘Manager’
FROM   customers
       LEFT JOIN offices ON offices.country = customers.country
WHERE  customers.country = 'USA'  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading