如果你有类似这些表格的东西:
CREATE TABLE Customer(
CustomerID SMALLSERIAL PRIMARY KEY,
Name VARCHAR(256) NOT NULL);
INSERT INTO Customer(Name) VALUES('Mr. Miller');
INSERT INTO Customer(Name) VALUES('Mr. Smith');
INSERT INTO Customer(Name) VALUES('Mr. John');
INSERT INTO Customer(Name) VALUES('Mr. Alex');
Create table Template(
TemplateID SMALLSERIAL PRIMARY KEY,
Description VARCHAR(256) NOT NULL);
INSERT INTO Template(Description) VALUES('Dear Mr. Smith, thank you for');
INSERT INTO Template(Description) VALUES('Dear Mr. John, thank you for');
INSERT INTO Template(Description) VALUES('Dear Mr. Alex, thank you for');
你可以使用这样的东西。
SELECT customer.Name, template.description, REPLACE(template.description, customer.Name, 'XXX')
FROM Customer INNER JOIN Template ON template.description LIKE CONCAT('%', Customer.name, '%');
结果是:
更新查询类似于:
WITH subquery AS (
SELECT customer.Name, template.description, REPLACE(template.description, customer.Name, 'XXX')
FROM Customer INNER JOIN Template ON template.description LIKE CONCAT('%', Customer.name, '%')
)
UPDATE template
SET description = subquery.replace
FROM subquery;