我需要一个mysql查询来更新数据(如果存在),否则插入没有唯一ID的数据。
其实我有一张这样的表
id company_ID Employee_ID Name Relationship Dob Age Gender
1 EMPL 00001 Choodamani Spouse 11-Aug-66 49 Female
2 EMPL 00001 Komala Mother 30-Oct-39 76 Female
3 EMPL 00001 Varshini Daughter 29-Apr-04 11 Female
4 EMPL 00001 Vasudevan Employee 15-Jul-62 53 Male
5 EMPL 00002 Siddharth Son 1-Jun-00 15 Male
6 EMPL 00002 Poongavanam Mother 21-Oct-39 76 Female
7 EMPL 00002 Aruna Spouse 16-Sep-68 47 Female
8 EMPL 00002 Abirami Daughter 7-May-97 18 Female
9 EMPL 00002 Murali Employee 7-Oct-67 48 Male
如果要插入这样的数据,
id company_ID Employee_ID Name Relationship Dob Age Gender
1 EMPL 00001 Choodamani Spouse 11-Aug-70 45 Female
2 EMPL 00001 Nirmal Son 30-Oct-39 76 Female
上面我更新了第一位员工Dob
和Age
,
在第二个id 2中,您可以看到我为同一个员工插入新数据
这里我需要的是,更新数据如果存在,否则插入新数据而不是唯一的,我需要查询此
在此表格中,我没有任何唯一字段,id
字段只有primary ke
y和auto increment
目前正在使用此查询插入
INSERT INTO employee (company_id, employee_id, name, relationship, dob, age, gender) VALUES ('$company_id','$employee_id', '$name', '$relationship', '$dob', '$age', '$gender')
答案 0 :(得分:1)
在两列employee_id
,name
和relationship
上创建一个唯一键:
CREATE UNIQUE INDEX empid_name ON employee (employee_id, name, relationship);
我在密钥中添加了relationship
,以防员工有一个与他们父亲同名的儿子。
然后,您可以使用INSERT INTO employee ... ON DUPLICATE KEY UPDATE ...
添加或更新行,具体取决于它是否已存在。
答案 1 :(得分:1)
您在评论中说明的唯一区别因素是姓名+ employee_id。这里有一些代码证明了这一点:
CREATE TABLE employee (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
company_ID varchar(32),
Employee_ID INT(11),
Name varchar(32),
Relationship varchar(32),
Dob varchar(32),
Age INT(11),
Gender varchar(32),
UNIQUE KEY employee_id_name (Employee_ID, Name)
);
INSERT INTO employee VALUES
(1, 'EMPL', 00001, 'Choodamani', 'Spouse', '11-Aug-66', 49, 'Female'),
(2, 'EMPL', 00001, 'Komala', 'Mother', '30-Oct-39', 76, 'Female'),
(3, 'EMPL', 00001, 'Varshini', 'Daughter', '29-Apr-04', 11, 'Female'),
(4, 'EMPL', 00001, 'Vasudevan', 'Employee', '15-Jul-62', 53, 'Male'),
(5, 'EMPL', 00002, 'Siddharth', 'Son', '1-Jun-00', 15, 'Male'),
(6, 'EMPL', 00002, 'Poongavanam', 'Mother', '21-Oct-39', 76, 'Female'),
(7, 'EMPL', 00002, 'Aruna', 'Spouse', '16-Sep-68', 47, 'Female'),
(8, 'EMPL', 00002, 'Abirami', 'Daughter', '7-May-97', 18, 'Female'),
(9, 'EMPL', 00002, 'Murali', 'Employee', '7-Oct-67', 48, 'Male');
INSERT INTO employee (company_id, employee_id, name, relationship, dob, age, gender) VALUES ('EMPL', 00001, 'Choodamani', 'Spouse', '11-Aug-70', 45, 'Female')
ON DUPLICATE KEY UPDATE company_id=VALUES(company_id), employee_id=VALUES(employee_id), name=VALUES(name), relationship=VALUES(relationship), dob=VALUES(dob), age=VALUES(age), gender=VALUES(gender);
INSERT INTO employee (company_id, employee_id, name, relationship, dob, age, gender) VALUES ('EMPL', 00001, 'Nirmal', 'Son', '30-Oct-39', 76, 'Female')
ON DUPLICATE KEY UPDATE company_id=VALUES(company_id), employee_id=VALUES(employee_id), name=VALUES(name), relationship=VALUES(relationship), dob=VALUES(dob), age=VALUES(age), gender=VALUES(gender);
在PHP中使用:
INSERT INTO employee (company_id, employee_id, name, relationship, dob, age, gender) VALUES ('$company_id','$employee_id', '$name', '$relationship', '$dob', '$age', '$gender') ON DUPLICATE KEY UPDATE company_id=VALUES(company_id), employee_id=VALUES(employee_id), name=VALUES(name), relationship=VALUES(relationship), dob=VALUES(dob), age=VALUES(age), gender=VALUES(gender)