 |
รบกวนช่วยดูโค๊ต UPDATE และ IF ให้หน่อยครับว่าผิดตรงไหน คือผมต้องการอัพเดทข้อมูลที่ได้จากการ query ลงอีก table หนึ่ง |
|
 |
|
|
 |
 |
|
รบกวนช่วยดูโค๊ต UPDATE และ IF ให้หน่อยครับว่าผิดตรงไหน คือผมต้องการอัพเดทข้อมูลที่ได้จากการ query ลงอีก table หนึ่ง โดยผมทำแบบนี้ครับ
1. ผมคิวรี่ข้อมูลชุดแรกได้ข้อมูล 5 รายการ แล้ว INSERT IN TO TABLE "A"
2. ผมคิวรี่ข้อมูลชุดที่ 2 ได้ข้อมูลมา 10 รายการ แล้วผมก็สั่ง update ลงใน TABLE "A" โดยถ้า shopcode ตรงกับข้อ 1 ก็ให้ update (ตรงจุดนี้ทำได้ครับ) แต่ที่ติดปัญหาคือ ถ้าเป็น shopcode ที่ได้มาใหม่ผมอยากให้มัน ADD INSERT IN TO TABLE "A" เพิ่มครับ ซึ่งผมลองแล้วมันไม่ได้ครับ โค๊ตที่ใช้ครับ
Code (PHP)
$strSQL4 = "SELECT SUM(total) AS sumtotal , stdrecord.shopcode ,user.branch FROM stdrecord INNER JOIN user On stdrecord.shopcode=user.user WHERE stdrecord.date = '".$addprintdate."' AND user.zone='".$objResult1["zone"]."' GROUP BY stdrecord.shopcode ";
$objQuery4 = mysql_query($strSQL4) or die ("Error Query [".$strSQL4."]");
$objQuery4 = mysql_query($strSQL4);
while($objResult4 = mysql_fetch_array($objQuery4))
{
$strSQL3 = "
INSERT INTO dailysale (shopcode,totalty,useram,branch)
VALUES
('".$objResult4["shopcode"]."','".$objResult4["sumtotal"]."','".$objResult1["zone"]."','".$objResult4["branch"]."')
";
mysql_query($strSQL3) or die(mysql_error());
}
$strSQL5 = "SELECT SUM(total) AS sumtotal , stdrecord.shopcode ,user.branch FROM stdrecord INNER JOIN user On stdrecord.shopcode=user.user WHERE stdrecord.date = '".$lastyear."' AND user.zone='".$objResult1["zone"]."' GROUP BY stdrecord.shopcode ";
$objQuery5 = mysql_query($strSQL5) or die ("Error Query [".$strSQL5."]");
$objQuery5 = mysql_query($strSQL5);
while($objResult5 = mysql_fetch_array($objQuery5))
{
$result = mysql_query("update dailysale set totally = '".$objResult5["sumtotal"]."' where shopcode = '".$objResult5["shopcode"]."'");
$objQuery = mysql_query($result);
if(!$objQuery)
{
$strSQL21 = "
INSERT INTO dailysale (shopcode,totally,useram,branch)
VALUES
('".$objResult5["shopcode"]."','".$objResult5["sumtotal"]."','".$objResult1["zone"]."','".$objResult5["branch"]."')
";
mysql_query($strSQL21) or die(mysql_error());
}
}
ขอคำแนะนำด้วยนะครับ
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2015-12-22 22:05:15 |
By :
sranuwat |
View :
822 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ไม่แน่ใจนะครับ เพราะผมใช้คำสั่ง mysql ไม่เป็น แต่ดูจากที่ท่านกล่าวมาน่าจะเป็นที่ช่วงนี้
Code (PHP)
while($objResult5 = mysql_fetch_array($objQuery5))
{
$result = mysql_query("update dailysale set totally = '".$objResult5["sumtotal"]."' where shopcode = '".$objResult5["shopcode"]."'");
$objQuery = mysql_query($result);
if(!$objQuery)
ลองเปลี่ยน if(!$objQuery) เป็น if(mysql_affected_rows() == 0) ดูครับ
|
 |
 |
 |
 |
Date :
2015-12-23 00:34:04 |
By :
noMerzy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยน if(!$objQuery) เป็น if(mysql_affected_rows() == 0) ดูแล้ว ไม่ได้ครับ มัน uddate อย่างเดี๋ยว แต่รายการที่เกินมันไม่ insert ให้ครับ
|
 |
 |
 |
 |
Date :
2015-12-23 08:34:11 |
By :
sranuwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูครับ เขียนสดไม่ได้เทสส
Code (PHP)
<?PHP
$strSQL4 = "SELECT SUM(total) AS sumtotal , stdrecord.shopcode ,user.branch FROM stdrecord INNER JOIN user On stdrecord.shopcode=user.user WHERE stdrecord.date = '".$addprintdate."' AND user.zone='".$objResult1["zone"]."' GROUP BY stdrecord.shopcode ";
$objQuery4 = mysql_query($strSQL4) or die ("Error Query [".$strSQL4."]");
while($objResult4 = mysql_fetch_array($objQuery4))
{
$strSQL3 = "
INSERT INTO dailysale (shopcode,totalty,useram,branch)
VALUES
('".$objResult4["shopcode"]."','".$objResult4["sumtotal"]."','".$objResult1["zone"]."','".$objResult4["branch"]."')
";
mysql_query($strSQL3) or die(mysql_error());
}
$strSQL5 = "SELECT SUM(total) AS sumtotal , stdrecord.shopcode ,user.branch FROM stdrecord INNER JOIN user On stdrecord.shopcode=user.user WHERE stdrecord.date = '".$lastyear."' AND user.zone='".$objResult1["zone"]."' GROUP BY stdrecord.shopcode ";
$objQuery5 = mysql_query($strSQL5) or die ("Error Query [".$strSQL5."]");
while($objResult5 = mysql_fetch_array($objQuery5))
{
$chkData = mysql_query("SELECT shopcode FROM dailysale where shopcode = '".$objResult5["shopcode"]."' ");
if(mysql_num_rows($chkData)>0){
mysql_query("update dailysale set totally = '".$objResult5["sumtotal"]."' where shopcode = '".$objResult5["shopcode"]."'");
} else {
$strSQL21 = "INSERT INTO dailysale (shopcode,totally,useram,branch) VALUES ('".$objResult5["shopcode"]."','".$objResult5["sumtotal"]."','".$objResult1["zone"]."','".$objResult5["branch"]."') ";
mysql_query($strSQL21) or die(mysql_error());
}
}
?>
|
 |
 |
 |
 |
Date :
2015-12-23 09:10:04 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
-ขอบคุณ (คุณคนธรรมดา ไม่พิเศษ) นะครับ สำหรับคำแนะนำ ตอนนี้ได้แล้วครับ
Code (PHP)
$strSQL5 = "SELECT SUM(total) AS sumtotal , stdrecord.shopcode ,user.branch FROM stdrecord INNER JOIN user On stdrecord.shopcode=user.user WHERE stdrecord.date = '".$lastyear."' AND user.zone='".$objResult1["zone"]."' GROUP BY stdrecord.shopcode ";
$objQuery5 = mysql_query($strSQL5) or die ("Error Query [".$strSQL5."]");
$objQuery5 = mysql_query($strSQL5);
while($objResult5 = mysql_fetch_array($objQuery5))
{
$strSQLchk = "SELECT * FROM dailysale WHERE shopcode = '".$objResult5["shopcode"]."' ";
$objQuerychk = mysql_query($strSQLchk);
$Num_Rowschk = mysql_num_rows($objQuerychk);
$objResultchk = mysql_fetch_array($objQuerychk);
if($Num_Rowschk > 0)
{
$result = mysql_query("update dailysale set totally = '".$objResult5["sumtotal"]."' where shopcode = '".$objResult5["shopcode"]."'");
$objQueryupdate = mysql_query($result);
}
else
{
$strSQL21 = "
INSERT INTO dailysale (shopcode,totally,useram,branch)
VALUES
('".$objResult5["shopcode"]."','".$objResult5["sumtotal"]."','".$objResult1["zone"]."','".$objResult5["branch"]."')
";
mysql_query($strSQL21) or die(mysql_error());
}
|
 |
 |
 |
 |
Date :
2015-12-23 14:20:44 |
By :
sranuwat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอติงนิดนึงนะครับ คำสั่งข้างล่าง query รอบเดียวก็พอครับ
Code (PHP)
$objQuery5 = mysql_query($strSQL5) or die ("Error Query [".$strSQL5."]");
$objQuery5 = mysql_query($strSQL5);
|
 |
 |
 |
 |
Date :
2015-12-23 15:42:45 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนคิวรี่ ครั้งแรก น่าจะ left join ตาราง a มาด้วย ถ้ามี ก็ update ถ้า ไม่มีก็ insert
Code (SQL)
select b.* , coalesce(a.id, 'new' ) as chk_a from b left join a on a.id=b.id
Code (PHP)
if( $ro['chk_a']=='new' ) insert into a
else update a set ........ where id='$ro['chk_a']
|
 |
 |
 |
 |
Date :
2015-12-23 18:28:30 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|