嗨欢迎来到Stack Overflow。
如果我明白你正确,你需要这样的事:
declare @customers table(
customer_no int NOT NULL,
matching_criteria varchar(50) NOT NULL,
lname varchar(50) NOT NULL,
keepID int NULL,
[status] varchar(50) NULL);
INSERT INTO @customers VALUES(1, 'a', 'Smith', null, null);
INSERT INTO @customers VALUES(2, 'a', 'Placeholder', null, null);
INSERT INTO @customers VALUES(3, 'b', 'Jones', null, null);
INSERT INTO @customers VALUES(4, 'b', 'Placeholder', null, null);
INSERT INTO @customers VALUES(5, 'c', 'Brown', null, null);
INSERT INTO @customers VALUES(6, 'c', 'Placeholder', null, null);
INSERT INTO @customers VALUES(7, 'd', 'Williams', null, null);
INSERT INTO @customers VALUES(8, 'd', 'Placeholder', null, null);
INSERT INTO @customers VALUES(9, 'e', 'Taylor', null, null);
INSERT INTO @customers VALUES(10, 'e', 'Placeholder', null, null);
SELECT * FROM @customers;
UPDATE @customers set [status] = case lname WHEN 'Placeholder' THEN 'DELETE' ELSE 'Keep' END;
SELECT * FROM @customers;
UPDATE k
SET keepID = d.customer_no
FROM
@customers k
INNER JOIN @customers d
on k.matching_criteria = d.matching_criteria
WHERE d.[status] = 'Delete' AND k.[status] = 'Keep';
SELECT * FROM @customers;
如果您使用的是SQL Server,您可以在查询窗口粘贴并观察结果。
请为将来的参考告诉我们你正在使用哪个数据库,因为答案不一样从db到db。以上是具体的SQL Server。虽然大多数dbs的表现会类似,但有些不会。以上更新加入。例如,Oracle不允许这样做,所以使用Oracle的语法不同 - Oracle当然可以实现同样的目的,它只是使用不同的SQL来实现。
你好...几件事情,你可以标记你的DBMS(SQL服务器,甲骨文,MySQL等)?另外,你可以发布你迄今为止尝试过的吗?最后,如果您将数据作为表格而不是图像发布在您的Q中,那么我们可以更轻松地处理这些数据。 – JohnHC
欢迎来到Stack Overflow。请花点时间参加[Tour](https://stackoverflow.com/tour)并阅读[how-to-ask](https://stackoverflow.com/questions/how-to-ask)以获取更好的回答你的问题 –