用另外两个 table 中的选定数据填充一个 table

Populate one table with selected data from two others tables

我有一个 table,我正在尝试用我们的公开票和他们收到的最后跟进填充,下面的列,这称为 "glpi_plugin_ns_followup":

ticket_number[int(11)], datetime_created[datetime], requester[int(11)], title[longtext], description[longtext], followup_datetime[datetime], last_followup[longtext], technician_user[int(11)]

我从中提取的 table 是 glpi_tickets 和 glpi_ticketfollowups

glpi_tickets

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_ticketfollowups

id[int(11)], tickets_id[int(11)], date[datetime], users_id[int(11)], content[longtext], is_private[tinyint(11)], requesttypes_id[int(11)]

三者的共同标识符如下:

glpi_tickets = id

glpi_ticketfollowups = tickets_id

glpi_plugin_ns_followup = ticket_number

我已使用以下查询成功插入前五列:

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;

但是我正在努力用相关更新填充最后 3 列,到目前为止我已经尝试使用此代码但它不起作用:

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)

这给了我回应:

Error Code: 1242. Subquery returns more than 1 row

当我尝试更新多行时应该这样做,或者错误可能是因为每张票有多个跟进。我有一种感觉,我可能从完全错误的角度来看待这个问题。如果有人可以帮助我并让我回到正确的轨道上,将不胜感激。

如果需要,我可以提供示例数据,但收集和审查它需要一段时间;-)

编辑:根据 Kamil 对 max() 的建议,我已经设法使日期字段正常工作:

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)

但是我无法让它为内容或 technician_user 工作,如果我可以拉出最新的 date/times 最后一次将跟进添加到票证中,我可以修改任何内容以拉入内容和 technician_user 还有?

EDIT2:尝试在您的初始解决方案末尾添加 GROUP BY t.id;,但它不使用最新的票据跟进。

EDIT3:尝试了以下,它拉入了正确的 date/time 但错误的内容

    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;

谢谢 iFr4g

您可以将这两个语句合并为一个且仅发出 INSERT 命令。但是,如果 glpi_ticketfollowups 中存在超过一个匹配行,这将使 glpi_tickets 中的行与条件 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)

如果您在目的地 table 中每个 glpi_tickets table 只需要一行,那么您正在寻找的是一种仅从 [=14= 获取一行的方法】 每一张票。这可以通过添加 GROUP BY 子句并选择适当的获取一个值的逻辑来实现,例如:max()min()。请记住,发出超过 3 个值的聚合函数不会为您带来一行的结果,而是整个数据集的聚合结果。这可以通过按照您开始的方式来避免 - 通过添加 LIMIT 子句插入和更新。


评论后编辑

由于您需要对每张票进行最后跟进,并且您确定每张票的日期都是唯一的,因此您可以在 glpi_ticketfollowups 中进行自连接计算每张票的最大日期,然后 LEFT JOIN对你的 glpi_tickets table:

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)