我想将两个不同的表行值插入到单个表中,
Posted
技术标签:
【中文标题】我想将两个不同的表行值插入到单个表中,【英文标题】:I want to insert two different table row values into single table, 【发布时间】:2014-12-23 08:55:29 【问题描述】:我有两个表“member_registration”和“mebership_details”使用外键从两个表中获取行值并移动到另一个表“existing_members”,原始行值被删除。
$query=mysql_query("existing_member(member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date,)
SELECT member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date FROM member_registration
WHERE member_id='".$memberid."'");
$query2=mysql_query("INSERT INTO existing_member (member_type) SELECT membership_type FROM membership_details
WHERE member_id='".$memberid."'");
if($query and $query2)
//echo "connected";
$sql=mysql_query("DELETE FROM member_registration WHERE member_id='".$memberid."'");
$sql1=mysql_query("delete from membership_details where member_id='".$memberid."'");
echo "<script>
alert('Data Deleted Successfully');
window.location.href='close_member.php';
</script>";
这是我的查询,单个查询,但是如何执行两个查询以将两个表中的值插入到单个表中,
【问题讨论】:
【参考方案1】:你可以试试联合
下面的例子
insert into table1(col1, col2, col3) (
select col1 as col1, col2 as col2, col3 as col3 from table2
union
select col1 as col1, col2 as col2, col3 as col3 from table3)
注意它可能已经存在的 id(主键或唯一),或者您可以使用插入忽略
希望这会有所帮助
【讨论】:
【参考方案2】:尝试像这样编写您的查询:
INSERT INTO Table ( Column1, Column2 ) VALUES ( Value1, Value2 ), ( Value1, Value2 )
这应该插入多条记录
如果表格总是在表格中包含两个值,并且您希望它们在两列中,您可以执行以下操作:
$query2=mysql_query("INSERT INTO existing_member (member_type) VALUES (SELECT membership_type FROM membership_details WHERE member_id='".$memberid."' LIMIT 1 OFFSET 0, SELECT membership_type FROM membership_details WHERE member_id='".$memberid."' LIMIT 1 OFFSET 1)");
只需重新阅读问题:
$query2=mysql_query("INSERT INTO existing_member (member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date) VALUES (SELECT member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date FROM member_registration WHERE member_id='".$memberid."')");
这就是你所追求的吗?
【讨论】:
我需要从两个表值复制并插入到一个表中,你能建议我吗?例如“member_registration”和“mebership_details”使用外键获取值,并插入“existing_members”表中。 如果我是正确的,那么这两个值都在membership_details 中是吗? 此表中的值是否也可能超过 2 个? 是的,有更多的值来自 members_details,like(membership_type,joining_date,expiry_date) 如果上述编辑没有帮助,请您提供更详细的信息,说明您希望通过此查询实现什么...【参考方案3】:尝试在 join 中使用类似的东西:
"INSERT INTO existing_member (member_type)
SELECT a.membership_type as member_type FROM membership_details a
INNER JOIN member_registration c ON c.member_id = a.member_id
where a.member_id='".$memberid."'"
【讨论】:
它工作正常,但是我如何在这个查询中插入“member_registration”值,“$query=mysql_query("INSERT INTO existing_member (member_id,member_name,dob,gender,address,city,state,电话、电子邮件、身高、体重、体重指数、血压、医疗、图像路径、加入日期) FROM member_registration WHERE member_id='".$memberid."'" ); " 您需要按照我在外键字段中提到的那样进行加入,并仅选择您想要插入的所需值。您已经在使用 member_registration 是否还想要membership_details 或什么。 只是我需要将“member_registration”和“membership_details”中的值插入“existing_members”表中,例如:insert from table1 values and table2 values into table3
贴出membership_details的所有列,我也会照着写sql。
答案中的 sql 是您提到的结构的演示。【参考方案4】:
这样,我试过这个代码,
`<?php
ini_set('display_errors', 0);
include 'connection/db_connection.php';
$memberid=$_POST['memberid'];
$query=mysql_query("INSERT INTO existing_membe(member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date)
SELECT member_id,member_name,dob,gender,address,city,state,phone,email,height,weight,bmi,bp,medical,image_path,joining_date FROM member_registration
WHERE member_id='".$memberid."'" );
/*
$query2=mysql_query("INSERT INTO existing_member (membership_type) SELECT membership_type FROM membership_details
WHERE member_id='".$memberid."'"); */
$query2=mysql_query("INSERT INTO existing_member (membership_type)
SELECT a.membership_type as membership_type FROM membership_details a
INNER JOIN member_registration c ON c.member_id = a.member_id, c.member_name=a.member_name
where a.member_id='".$memberid."'");
if($quer and $query2)
$sql=mysql_query("DELETE FROM member_registration WHERE member_id='".$memberid."'");
$sql1=mysql_query("delete from membership_details where member_id='".$memberid."'");
echo "<script>
alert('Data Deleted Successfully');
window.location.href='close_member.php';
</script>";
else
echo "User Data Already Exist";
?>`
【讨论】:
以上是关于我想将两个不同的表行值插入到单个表中,的主要内容,如果未能解决你的问题,请参考以下文章