我有一个查询(MySQL),它从同一个数据库中的 4 个表中提取数据。我想做的是运行查询并在发生更改时更新结果并将新记录插入另一个数据库中的单独表中。
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
上面的查询是从我们的票务系统中提取数据。
我能够使用以下方法初始插入数据:
INSERT INTO Support (Created, TicketID, CompanyName, Subject, Queue, Owner, Status, LastUpdated, Location, Timeworked, OverRide, Resolved)
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
但是,当尝试更新已经存在的数据或添加其他新数据时,我会出错。
UPDATE Support
SELECT a.Created,
a.id 'TicketID',
GROUP_CONCAT((CASE WHEN d.CustomField = 1 THEN d.Content ELSE NULL END)) `CompanyName`,
a.Subject,
c.Name Queue,
b.Name 'Owner',
a.`Status`,
a.LastUpdated,
GROUP_CONCAT((CASE WHEN d.CustomField = 4 THEN d.Content ELSE NULL END)) `Location`,
a.TimeWorked 'TimeWorked',
GROUP_CONCAT((CASE WHEN d.CustomField = 2 THEN d.Content ELSE NULL END)) `OverRide`,
a.Resolved
FROM rt.Tickets a
INNER JOIN rt.Users b
ON a.owner = b.id
INNER JOIN rt.Queues c
ON a.queue = c.id
INNER JOIN rt.ObjectCustomFieldValues d
ON a.id = d.ObjectID
GROUP BY a.id
谢谢,