我正在尝试从另一个表更新我的表。我想忽略重复项并删除该表中的所有匹配项。
Source Table Table 1 Table 2
Customer | Address Customer | Address Customer | Address
__________________ __________________ __________________
Mike 123 Main Mike 123 Main Bob 999 1st
Steve 456 Maple Steve 456 Maple
John 789 Elm John 789 Elm
Bob 999 1st
例如,表 2 仅在“源表”与表 1 进行比较后才会有唯一条目。我使用的是 MYSQL/PHP。
非常感谢任何方向。
最佳答案
MySQL 5.5.32 架构设置:
CREATE TABLE Source
(`Customer` varchar(5), `Address` varchar(9))
;
INSERT INTO Source
(`Customer`, `Address`)
VALUES
('Mike', '123 Main'),
('Steve', '456 Maple'),
('John', '789 Elm'),
('Bob', '999 1st'),
('Bob', '999 1st')
;
CREATE TABLE Table1
(`Customer` varchar(5), `Address` varchar(9))
;
INSERT INTO Table1
(`Customer`, `Address`)
VALUES
('Mike', '123 Main'),
('Steve', '456 Maple'),
('John', '789 Elm')
;
CREATE TABLE Table2
(`Customer` varchar(5), `Address` varchar(9))
;
DELETE FROM Table2;
INSERT INTO Table2
(`Customer`, `Address`)
SELECT DISTINCT Customer, Address
FROM Source
WHERE NOT EXISTS (SELECT 1 FROM Table1 WHERE Source.Customer = Table1.Customer)
查询 1:
SELECT *
FROM Table2
| CUSTOMER | ADDRESS |
|----------|---------|
| Bob | 999 1st |
关于php - MYSQL 创建具有唯一值的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28420120/