1

This is my exact SQL code, but it's very slow and I know it's terrible...

Is there another way to do this but faster?

UPDATE Customer SET Title = "MR", SurName = "Test", FirstName ="Bob", HomePostCode ="SW18" WHERE CustomerID IN (SELECT CxID FROM Production WHERE policy_number = "001C5T" AND member_number = "100" AND family_group = "000001" AND CxID > 0)

Thanks

4

5 回答 5

3

试试这个(未经测试)

UPDATE Customer c
JOIN Production p ON p.CxID = c.CustomerID
SET c.Title = "MR",
    c.SurName = "Test",
    c.FirstName ="Bob",
    c.HomePostCode ="SW18"
WHERE p.policy_number = "001C5T"
    AND p.member_number = "100"
    AND p.family_group = "000001"
    AND p.CxID > 0
于 2012-07-03T13:55:31.570 回答
1

you can join the tables in the update, so there will be no subselect.

于 2012-07-03T13:49:12.513 回答
1

我想你想要这样的东西:

UPDATE customer 
SET Title = "MR", SurName = "Test", FirstName ="Bob", HomePostCode ="SW18" 
FROM customer    
inner join production on customer.customerID = production.cxid 
where policy_number = "001C5T" AND member_number = "100" AND family_group = "000001" AND CxID > 0
于 2012-07-03T13:55:33.847 回答
1

要扩展 krakover 的答案,您可以加入表格:

UPDATE Customer JOIN 
Production P ON (C.CustomerID = P.CxID) 
AND (P.policy_number = "001C5T") 
AND (P.member_number = "100") 
AND (P.family_group = "000001") 
AND (P.CxID > 0)  
SET C.Title = "MR", C.SurName = "Test", C.FirstName ="Bob", C.HomePostCode ="SW18"

请注意,请根据您的 shema 仔细检查表别名。

于 2012-07-03T13:56:13.393 回答
0

另一种没有 JOIN 的方法:

INSERT INTO Customer (CustomerID, Title, SurName, FirstName, HomePostCode)
SELECT CxID, 'MR', 'Test', 'Bob', 'SW18'
FROM Production
WHERE policy_number = '001C5T' AND member_number = '100' AND family_group = '000001' AND CxID > 0
ON DUPLICATE KEY UPDATE Title = VALUES(Title), SurName = VALUES(SurName), FirstName = VALUES(FirstName), HomePostCode = VALUES(HomePostCode)

这将要求您拥有并选择至少一个唯一列。我假设 CustomerID 是。

于 2012-07-03T14:01:36.000 回答