Jump to content
Destramic

select and insert

Recommended Posts

im unable to find an example when it comes to select and insert in my particular case, so any help would be greatly appreciated.

here is my 2 select queries which retrieve the department_id and task_type_id.  what i want to do it is use those two values inside an insert as well as adding additional values.

 

SELECT department_id FROM departments WHERE department = "Gas Servicing"
SELECT task_type_id FROM task_Types WHERE task_type = "Gas Service"

INSERT INTO property_appointments (property_id, department_id, task_type_id, notes, priority_days) 
                           VALUES (:property_id, ?, ?, :notes, 1)

 

is this possible please?

 

thank you

Share this post


Link to post
Share on other sites

Why are you in a position where the department and task_type ids are not known? Are you passing the names from an input form instead of the ids?

Share this post


Link to post
Share on other sites

I would n't use an INSERT  ... SELECT statement in this case. Just an normal insert with a couple of subqueries

INSERT INTO property_appointments (property_id, department_id, task_type_id, notes, priority_days) 
VALUES ( :property_id
         , (SELECT department_id FROM departments WHERE department = :deptname)
         , (SELECT task_type_id FROM task_Types WHERE task_type = :taskname)
         , :notes
         , 1 );

 

Share this post


Link to post
Share on other sites

i can pass the relevant ID's over infact, i don't know what i didnt do that instead of complicating things!

the simpliest ways are always the best. :suicide:

thank you for the insight on how i could of done the query also.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.