P
P
Protein_R2018-06-07 21:24:56
SQL
Protein_R, 2018-06-07 21:24:56

How to perform an INSERT with a subquery?

There are 2 tables: auto and parking_time
5b197817565cf333500942.png
Need to INSERT into parking_time. The problem is that there is all the data except auto.id, but there is auto.car_number.
In theory, you need to execute an INSERT query with a SELECT subquery to get the car id. How to implement it correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexalexes, 2018-06-07
@Protein_R

https://www.w3schools.com/sql/sql_insert_into_sele...
See the last example.

insert into  parking_time (owner_id, auto_id, parker_id, place_id, begin_date, exp_date)
select :owner_id, a.id, :parker_id, :place_id, :begin_date, :exp_date
from auto a
where a.car_number = :car_number

You still have dependencies on owner_id and place_id there, so the select will be even more complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question