mysql join inside insert into

I have to make a MySQL query, and I´m kind of trapped.

I have three tables: usuarios, combis, and usuario_has_combis. I make some SELECTs to check if a user hasn´t reserved a transport yet. Up to that point it´s okay. But I need to query that, if user hasn´t booked a transport yet, to book it, and I have something like this:

$insertaPedido = "INSERT INTO usuarios_has_combis (id_usuarios,id_combis,fecha) VALUES ('$user','$combi','$fecha')";

The thing is that this combined row is connected with both the usuarios table and combis table. I would like to know, where to write the join in this query.

Upvotes: 0

Views: 87

Answers (1)

Barmar
Barmar

Reputation: 780861

Not sure if I understand you, but I think this is what you want:

INSERT into usuarios_has_combis (id_usuarios,id_combis,fecha)
SELECT id_usuarios, id_combis, '$fecha'
FROM usuarios, combis
WHERE user = '$user'
AND combi = '$combi'

You shouldn't actually interpolate variables into the statement, you should use a prepared statement with placeholders. This just shows the structure.

Upvotes: 1

Related Questions