简体   繁体   English

MYSQL使用两个不同表的结果插入到单个表中

[英]MYSQL insert into single table using the results of two different tables

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 . 我希望将这些值插入到表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. machtiging的是插入标准3,但我需要能够改变这种状况,以及为1,如果公司已经存在,但我会拦截,在我的代码另一部分。

All the examples I saw were just the results from one table being imported into anther table. 我看到的所有示例都只是将一个表导入到anther表中的结果。 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. 因此,为了使问题简短:我如何获得bedrijfs_idklant_id到相应的列到klant_bedrijf_machtiging同时仍自己能够操纵的最后一列machtiging在短短1查询。

Good news! 好消息! MySQL has an easy way to do this: MySQL Insert Into w/ Select MySQL有一个简单的方法: MySQL插入w / Select

So in your case it would be: 所以在你的情况下它将是:

INSERT INTO klant_bedrijf_machtiging (`klant_id`, `bedrijfs_id`, `machtiging`)
SELECT `klanten`.`klant_id`, `klanten_zakelijk`.`bedrijfs_id`, 3
    FROM `klanten`,`klanten_zakelijk`
    WHERE `klanten`.`emailadres` = '$email' 
    AND `klanten_zakelijk`.`bedrijfs_kvk`='$kvknr'

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. 至于将项目更改为1(如果它已经存在),我不确定如何在一个查询中执行该操作而不会造成大量混乱。

INSERT INTO `klant_bedrijf_machtiging` (`klant_id`, `bedrijfs_id`, `machtiging`)
(SELECT `klanten`.`klant_id`, `klanten_zakelijk`.`bedrijfs_id`, 3
FROM `klanten`,`klanten_zakelijk`
WHERE `klanten`.`emailadres` = '$email'
AND `klanten_zakelijk`.`bedrijfs_kvk`='$kvknr')
INSERT INTO yourtable (fields) SELECT `klanten_zakelijk`.`bedrijfs_id`, `klanten`.`klant_id` 
    FROM `klanten`,`klanten_zakelijk`
    WHERE `klanten`.`emailadres` = '$email' 
    AND `klanten_zakelijk`.`bedrijfs_kvk`='$kvknr'
INSERT INTO klant_bedrijf_machtiging (`klant_id`, `bedrijfs_id`, `machtiging`)
SELECT `klanten`.`klant_id`, `klanten_zakelijk`.`bedrijfs_id`, 3
    FROM `klanten`,`klanten_zakelijk`
    WHERE `klanten`.`emailadres` = '$email' 
    AND `klanten_zakelijk`.`bedrijfs_kvk`='$kvknr'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM