我有两个表" 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>";
}
这是我的查询,单个查询但如何执行两个查询以将两个表中的值插入到单个表中,
答案 0 :(得分: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(主键或唯一),或者你可以使用insert ignore
希望这会有所帮助
答案 1 :(得分:0)
尝试编写您的查询:
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."')");
这就是你想要的吗?
答案 2 :(得分:0)
尝试使用加入:
这样的内容"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."'"
答案 3 :(得分:0)
这样,我尝试了这段代码,
`<?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";
}
?>`