$date = date('Y-m-d');
$query = 'UPDATE A SET SO_Date = :Date, L_Date = now(), A.Sum_So_Qty = IFNULL(B.Sum_So_Qty, 0), A.Sum_So_Amt = IFNULL(B.Sum_So_Amt, 0)'
. ' FROM tso A'
. ' LEFT JOIN (SELECT So_Id, SUM(So_Qty) AS Sum_So_Qty '
. ' , SUM(So_Amt) AS Sum_So_Amt'
. ' FROM tsodetail '
. ' GROUP BY So_Id'
. ' ) B ON A.So_Id = B.So_Id'
. ' WHERE A.So_Id = :So_Id';
$sth = $this->db->prepare($query);
$sth->execute(array(':Date' => $date,':So_Id' => $So_Id)) or die("Can't update header.");
จาก Code ด้านบน เป็นการเขียนในรูปแบบ ของ SQL Server
แต่เมื่อ Run ใน Mysql แจ้งว่าผิด Syntax
Code
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM tso A LEFT JOIN (SELECT So_Id, SUM(So_Qty) AS Sum_So_Qty , SUM(So_Amt) AS S' at line 1
UPDATE tso A LEFT JOIN (SELECT So_Id, SUM(So_Qty) AS Sum_So_Qty , SUM(So_Amt) AS Sum_So_Amt FROM tsodetail GROUP BY So_Id ) B ON A.So_Id = B.So_Id SET So_No = :So_No , So_Date = :Date, L_Date = now(), A.Sum_So_Qty = IFNULL(B.Sum_So_Qty, 0), A.Sum_So_Amt = IFNULL(B.Sum_So_Amt, 0) WHERE A.So_Id = :So_Id