Populate one table with selected data from two others tables
<p>I have a table that I am trying to populate with our open tickets and the last followup they received, columns below, this is called "glpi_plugin_ns_followup":</p>

<pre><code>ticket_number[int(11)], datetime_created[datetime], requester[int(11)], title[longtext], description[longtext], followup_datetime[datetime], last_followup[longtext], technician_user[int(11)]
</code></pre>

<p>The tables I am pulling from are glpi_tickets and glpi_ticketfollowups</p>

<p>glpi_tickets</p>

<pre><code>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)]
</code></pre>

<p>glpi_ticketfollowups</p>

<pre><code>id[int(11)], tickets_id[int(11)], date[datetime], users_id[int(11)], content[longtext], is_private[tinyint(11)], requesttypes_id[int(11)]
</code></pre>

<p>The common identifier across all three are as follows:</p>

<p>glpi_tickets = id</p>

<p>glpi_ticketfollowups = tickets_id</p>

<p>glpi_plugin_ns_followup = ticket_number</p>

<p>I've managed to insert the first five columns by using the below query:</p>

<pre><code>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;
</code></pre>

<p>However I am struggling to fill in the last 3 columns with the relevant updates, so far I have tried using this code but it doesn't work:</p>

<pre><code>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)
</code></pre>

<p>This gives me the response:</p>

<pre><code>Error Code: 1242. Subquery returns more than 1 row
</code></pre>

<p>Which it should as I'm trying to update multiple rows, or the error may be because there are multiple followups per ticket. I have a feeling I may be going at this from entirely the wrong angle. If someone can assist me and put me back on the right track that would be much appreciated.</p>

<p>I can supply sample data if needed but it will take a while to gather and censor it ;-)</p>

<p>EDIT: As per Kamil's suggestions with max() I have managed to get the date fields to work:</p>

<pre><code>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)
</code></pre>

<p>However I cannot get it to work for content or technician_user, if I can pull the newest date/times for the last time a followup was added to a ticket can I modify anything to pull in the content and technician_user as well?</p>

<p>EDIT2: tried adding <code>GROUP BY t.id;</code> at the end of your initial solution but it does not use the newest followup for the tickets.</p>

<p>EDIT3:Have tried the following, it pulls in the correct date/time but the wrong content</p>

<pre><code>    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;
</code></pre>

<p>Thanks
iFr4g</p>