Hello to whomever wishes to bite his teeth on this,
I came across multiple examples of how to use the result set of subquery select into another table.
What I have however is:
SELECT `klanten_zakelijk`.`bedrijfs_id`, `klanten`.`klant_id`
FROM `klanten`,`klanten_zakelijk`
WHERE `klanten`.`emailadres` = '$email'
AND `klanten_zakelijk`.`bedrijfs_kvk`='$kvknr'
As you can see I take the results from two different tables and need to port them to another table.
I wish to insert those values into the table klant_bedrijf_machtiging.
INSERT INTO `klant_bedrijf_machtiging` (`klant_id`, `bedrijfs_id`, `machtiging`) VALUES ('8501', '1', '3');
The column machtiging is is standard 3 with an insert but I need to be able to change that as well to 1 if the company already exists, but I will intercept that at another part of my code.
All the examples I saw were just the results from one table being imported into anther table.
Does that same logic apply when you take results from two tables? If so, what would be the wisest and most efficient way to implement this?
So to make the question short:
How do I get bedrijfs_id and klant_id into the corresponding columns into klant_bedrijf_machtiging whilst still myself being able to manipulate the last column machtiging in just 1 query.
Good news! MySQL has an easy way to do this: MySQL Insert Into w/ Select
So in your case it would be:
As for changing the item to 1 if it already exists, I am not sure how you would do that in one query without making a huge mess of it.