这不是一个重复的问题。上一个问题与PDO无关。我在mysql中有两个表:
USERS
-------------------------------------
employeeid | name | saving | salary
-------------------------------------
12 | Bob | 100 | 1000
23 | Joe | 50 | 800
USERS table
employeeid
name
saving
salary
和
EMPLOYEE
-----------------------------------
id | managerid | workerid
-----------------------------------
1 | 12 | 23
EMPLOYEE table
id
managerid FOREIGN KEY
workerid FOREIGN KEY
1-(经理和工人都是员工)为了更新工人的保存字段(比如+ 10美元),现场工资需要更新 - $ 10
2-输入变量来自PHP表单name
,因此逻辑流程为:
name > find employeeid (id) from USERS > find managerid (id2) from EMPLOYEE > find employeeid (id3) from USERS > update saving and salary
所以sql语句可以单独写成:
id = SELECT employeeid FROM USERS WHERE name = $name; //find id of employee in USERS
id2 = SELECT managerid FROM EMPLOYEE WHERE workerid = id; //find id of worker in EMPLOYEE
UPDATE USERS SET saving = saving + 10, salary = salary -10 WHERE employeeid = id2;
是否可以在一个(PDO格式)中执行这3个语句。 上面的msql PDO格式(用PHP):
$sql = "SELECT employeeid FROM USERS WHERE name=:namepara";
$sttm = prepare($sql);
$sttm->execute(array(":namepara"=>$name));
$row=$sttm->fetch(PDO::FETCH_ASSOC);
$sql2 = "SELECT managerid FROM EMPOYEE WHERE workerid=:idpara";
$sttm2 = prepare($sql2);
$sttm2->execute(array(":idpara"=>$row['employeeid']));
$row2=$sttm2->fetch(PDO::FETCH_ASSOC);
$sql3 = "UPDATE USERS SET saving = saving + 10, salary = salary - 10 WHERE
employeeid=:id2para";
$sttm3 = prepare($sql3);
$sttm3->execute(array(":id2para"=>$row2['managerid']));
$row3=$sttm3->fetch(PDO::FETCH_ASSOC);
任何帮助将不胜感激!
答案 0 :(得分:2)
$sql = "UPDATE USERS JOIN EMPLOYEE ON USERS.employeeid = EMPLOYEE.managerid
SET Saving = saving +10, salary = salary - 10
WHERE USERS.name = :namepara";
$sttm = prepare($sql);
$sttm->execute(array(":namepara"=>$name));
$row=$sttm->fetch(PDO::FETCH_ASSOC);