我有一张桌子,我正在尝试使用我们的打开的门票和他们收到的最后一次跟进,下面的列,这称为“glpi_plugin_ns_followup”:
id[int(11)], entities_id[int(11)], name[varchar(255)], date[datetime], closedate[datetime], solvedate[datetime], date_mod[datetime], users_id_lastupdater[int(11)], status[int(11)], users_id_recipient[int(11)], requesttypes_id[int(11)], content, urgency[int(11)], impact[int(11)], priority[int(11)], itilcategories_id[int(11)], type[int(11)], solutiontypes_id[int(11)], solution, global_validation[int(11)], slas_id[int(11)], slalevels_id[int(11)], due_date[datetime], begin_waiting_date[datetime], sla_waiting_duration[int(11)], waiting_duration[int(11)], close_delay_stat[int(11)], solve_delay_stat[int(11)], takeintoaccount_delay_stat[int(11)], actiontime[int(11)], is_deleted[tinyint(1)], locations_id[int(11)], validation_percent[int(11)]
我从中拉出的表是glpi_tickets和glpi_ticketfollowups
glpi_tickets
id[int(11)], tickets_id[int(11)], date[datetime], users_id[int(11)], content[longtext], is_private[tinyint(11)], requesttypes_id[int(11)]
glpi_ticketfollowups
TRUNCATE glpi_plugin_ns_followup;
INSERT INTO glpi_plugin_ns_followup (ticket_number, datetime_created, requester, title, description)
SELECT Id, date, users_id_recipient, name, content FROM glpi.glpi_tickets
WHERE is_deleted = 0 and status !=6 and status !=5;
这三个公共标识符如下:
glpi_tickets = id
glpi_ticketfollowups = tickets_id
glpi_plugin_ns_followup = ticket_number
我设法使用以下查询插入前五列:
UPDATE glpi_plugin_ns_followup
SET followup_datetime = (SELECT date from glpi_ticketfollowups where glpi_ticketfollowups.tickets_id = glpi_plugin_ns_followup.ticket_number),
last_followup = (SELECT content from glpi_ticketfollowups where glpi_ticketfollowups.tickets_id = glpi_plugin_ns_followup.ticket_number),
technician_user = (SELECT users_id from glpi_ticketfollowups where glpi_ticketfollowups.tickets_id = glpi_plugin_ns_followup.ticket_number)
但是我很难用相关更新填写最后3列,到目前为止我已经尝试使用此代码,但它不起作用:
Error Code: 1242. Subquery returns more than 1 row
这给了我答复:
UPDATE glpi_plugin_ns_followup
SET followup_datetime = (SELECT max(date) from glpi_ticketfollowups where glpi_ticketfollowups.tickets_id = glpi_plugin_ns_followup.ticket_number GROUP BY tickets_id)
我应该尝试更新多行,或者错误可能是因为每张票有多个后续。我有一种感觉,我可能会从完全错误的角度出发。如果有人可以帮助我并让我回到正确的轨道上,那将非常感激。
如果需要,我可以提供样本数据,但需要一段时间来收集和审查它; - )
编辑:根据卡米尔对max()的建议,我设法让日期字段起作用:
GROUP BY t.id;
但是,我无法让它为内容或技术人员用户工作,如果我可以在最后一次为故障单添加后续操作时提取最新的日期/时间,我是否可以修改任何内容以提取内容和技术员_用户? / p>
EDIT2:尝试在初始解决方案的最后添加 TRUNCATE glpi_plugin_ns_followup;
INSERT INTO glpi_plugin_ns_followup (ticket_number, datetime_created, requester, title, description, followup_datetime, last_followup, technician_user)
SELECT
t.id, t.date, t.users_id_recipient, t.name, t.content,
tf.datetime, tf.content, tf.users_id
FROM
glpi.glpi_tickets t
LEFT JOIN (SELECT max(date) as datetime, content, users_id, tickets_id
FROM glpi_ticketfollowups
GROUP BY tickets_id) tf ON
t.id = tf.tickets_id
WHERE
t.is_deleted = 0
and t.status NOT IN (5,6)
GROUP BY t.id;
,但它没有使用最新的门票跟进。
EDIT3:尝试过以下内容,它会输入正确的日期/时间但内容错误
{{1}}
由于 iFr4g
答案 0 :(得分:0)
您可以将这两个语句合并为一个且仅发出INSERT
命令。但是,如果使用条件glpi_tickets
获取的glpi_ticketfollowups
中存在多个匹配行,则会将您的行与t.id = tf.tickets_id
相乘。
我添加了LEFT JOIN
并略微更改了WHERE
子句以节省一些编码。
INSERT INTO glpi_plugin_ns_followup (ticket_number, datetime_created, requester, title, description, followup_datetime, last_followup, technician_user)
SELECT
t.id, t.date, t.users_id_recipient, t.name, t.content,
tf.date, tf.content, tf.users_id
FROM
glpi.glpi_tickets t
LEFT JOIN glpi_ticketfollowups tf ON
t.id = tf.tickets_id
WHERE
t.is_deleted = 0
and t.status NOT IN (5,6)
如果目标表中每个glpi_tickets
表只需要一行,那么您要查找的是每个票证只能从glpi_ticketfollowups
获取一行的方法。这可以通过添加GROUP BY
子句并选择获取一个值的适当逻辑来实现,例如:max()
或min()
。请记住,发布超过3个值的聚合函数不会从一行获得结果,而是从整个数据集中获得聚合结果。这可以通过您开始的方式来避免 - 通过添加LIMIT子句来插入和更新。
评论后编辑:
由于您需要为每张故障单进行最后一次跟进,并且您确定每张故障单的日期都是唯一的,您可以自行加入计算glpi_ticketfollowups
中每张故障单的最大日期,然后LEFT JOIN
你的glpi_tickets
表:
INSERT INTO glpi_plugin_ns_followup (ticket_number, datetime_created, requester, title, description, followup_datetime, last_followup, technician_user)
SELECT
t.id, t.date, t.users_id_recipient, t.name, t.content,
tf.date, tf.content, tf.users_id
FROM
glpi.glpi_tickets t
LEFT JOIN (
SELECT allf.tickets_id, allf.date, allf.content, allf.users_id
FROM glpi_ticketfollowups allf
INNER JOIN (
SELECT tickets_id, MAX(date) AS date
FROM glpi_ticketfollowups
GROUP BY tickets_id
) lastf ON
allf.tickets_id = lastf.tickets_id
AND allf.date = lastf.date
) tf ON
t.id = tf.tickets_id
WHERE
t.is_deleted = 0
and t.status NOT IN (5,6)