php中如何实现B表没有A表aid=3的数据就插入,有则更新部分字段
A表Aidshopnametime1aa1aa12bb2bb23cc2cc2B表BidAidname11a122b2...
A表Aid shop name time
1 a a1 aa1
2 b b2 bb2
3 c c2 cc2
B表Bid Aid name
1 1 a1
2 2 b2 展开
1 a a1 aa1
2 b b2 bb2
3 c c2 cc2
B表Bid Aid name
1 1 a1
2 2 b2 展开
展开全部
$sql = "select * from b where aid = 3";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
if(count($row)>1){
$sql = "update .....";
}else{
$sql = "insert .....";
}
或者直接写sql
insert into b(bid,aid,name) values(3,3,'xxxx') on DUPLICATE KEY UPDATE bid=3,.....
DUPLICATE KEY效率很低 一般我不用
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
if(count($row)>1){
$sql = "update .....";
}else{
$sql = "insert .....";
}
或者直接写sql
insert into b(bid,aid,name) values(3,3,'xxxx') on DUPLICATE KEY UPDATE bid=3,.....
DUPLICATE KEY效率很低 一般我不用
展开全部
$sql = "select * from b where aid = 3";
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
if(count($row)!=0){
$sql = "update B set `name` = ".$row[0]['name']."where Aid = 3";
mysql_query($sql);
}else{
$sql = "insert into B values('3',".$row[0]['Aid'].",".$row[0]['name'].")";
mysql_query($sql);
相关函数可参阅
http://www.w3cschool.cn/php_ref_mysql.html
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
if(count($row)!=0){
$sql = "update B set `name` = ".$row[0]['name']."where Aid = 3";
mysql_query($sql);
}else{
$sql = "insert into B values('3',".$row[0]['Aid'].",".$row[0]['name'].")";
mysql_query($sql);
相关函数可参阅
http://www.w3cschool.cn/php_ref_mysql.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询