如何在两个表中插入值在一个表中的值等于另一个表中的值?

时间:2016-06-27 08:46:10

标签: php mysql

这里我有两个表pro_zone_m和temp_const,我将值插入第三个表pro_m,我将从temp_const插入区域,我需要从pro_zone_m获取相应区域的zone_id的值。 我尝试但无法想出解决方案。

$uisq = "SELECT * FROM pro_zone_m";
$ytu = mysql_query($uisq);
$mnm = mysql_fetch_array($ytu);
extract($mnm);


$wqr = "SELECT * FROM temp_const";
$wqe = mysql_query($wqr);
while($wqe1 = mysql_fetch_array($wqe))
{
extract($wqe1);


$zxc = "INSERT INTO pro_m set custid='$custid', firstname='$firstname',     lastname='$lastname', country='$country', 
city='$city', state='$state', zone='$zone', adresstype='$address_type',     address1='$address1', address2='$address2',category='$category', 
focus='$focus', assignto='$assign_to', division='$division',     product='$product', grade='$grade', potential='$potential',     designtn='$designation',
mobile='$mobile', primphone='$primary_phone', primemail='$primary_email',
exhibition = '$exhibition',  zonea_id = '$zoneid' WHERE zone = '$code' ";

$zxc1 = mysql_query($zxc);

1 个答案:

答案 0 :(得分:0)

更新您的插入查询,如下所示:(假设zonea_id将存储temp_const表中的zone_id。

$ zxc =" INSERT INTO pro_m set custid =' $ custid',firstname =' $ firstname',lastname =' $ lastname',国家=' $国家&#39 ;, city =' $ city',state =' $ state',zone =' $ zone',adresstype =' $ address_type',address1 = ' $ address1',address2 =' $ address2',category =' $ category', 焦点=' $ focus',assignto =' $ assign_to',division =' $ division',product =' $ product',grade = ' $ grade',potential =' $ potential',designtn =' $ designation', mobile =' $ mobile',primphone =' $ primary_phone',primemail =' $ primary_email', exhibition =' $ exhibition', zonea_id =(SELECT zone_id FROM temp_const where zone =' $ code')";