使用已加入表进行更新

时间:2015-08-17 16:04:22

标签: php mysql sql

我这里有两个表结构:

employee 
(emp_id INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(100),
emp_ic VARCHAR(20),
emp_phone VARCHAR(20),
emp_address VARCHAR(200))

employee_medical
(id INT AUTO_INCREMENT PRIMARY KEY,
emp_id INT,
medical_notes TEXT)

这是我选择表格的方式:

$query ="SELECT * FROM employee where emp_id ='$id'";

我输入的代码:

$sql = "UPDATE employee,employee_medical SET employee.emp_name='a' , employee.emp_ic='b' 
, employee.emp_phone='1', employee.emp_address='1' , employee_medical.medical_notes='1'  WHERE  emp_id=$id";

只有medical_notes没有显示。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您需要将两个表连接在一起才能同时更新这两个表。试试这个:

$sql = "UPDATE employee 
INNER JOIN employee_medical ON employee.emp_id = employee_medical.emp_id
SET employee.emp_name = 'a', employee.emp_ic = 'b', employee.emp_phone = '1', employee.emp_address = '1', employee_medical.medical_notes = '1' 
WHERE employee.emp_id = '" . $id . "'";