title: Trigger to insert data based on a value
body: <p>We have 3 tables:</p>
  
  <p>punches-table :</p>
  
  <pre><code>ID  user_code (columns )
  2   10
  </code></pre>
  
  <p>Employees_table :</p>
  
  <pre><code>Id  user_code(columns )
  5   10
  </code></pre>
  
  <p>I want a trigger that once a new record is inserted to punches_table will search for the same user_code in employees table and than insert the 2 id values into: </p>
  
  <pre><code>Punches-employees-relation_table:
  
  ID  employees_id     punches_id    
  1   2                 5
  </code></pre>
  
  <h2>Update</h2>
  
  <p>Thanks for the answer, I am getting 1064 error, I guess I have MariaDB and the code is not good for MariaDB.</p>
  
  <p>Here is what I am trying to do: First I would like to say that at the first time I did not put the exact tables and fields names.</p>
  
  <p>So to put thing in order: The first table name is a123_employees_punches </p>
  
  <pre><code>Fields:
  id (char(36))
  user_code (int(255))
  </code></pre>
  
  <p>The 2nd table is abc12_employees_cstm</p>
  
  <pre><code>Fields:
  id_c (char(36))
  usercode_c (varchar(255))
  </code></pre>
  
  <p>The 3rd table is abc12_employees_a123_employees_punches_1_c</p>
  
  <pre><code>fields:
  abc12_employees_a123_employees_punches_1abc12_employees_ida (varchar(36))
  abc12_empl4863punches_idb (varchar(36))
  </code></pre>
  
  <p>Environment:</p>
  
  <pre><code>Server: Localhost via UNIX socket
  Server type: MariaDB
  Server version: 5.5.64-MariaDB - MariaDB Server
  Protocol version: 10
  User: root@localhost
  Server charset: UTF-8 Unicode (utf8)
  </code></pre>
  
  <p>The goal is to have a trigger that once a new record is inserted into a123_employees_punches table to obtain the matching id_c from abc12_employees_cstm table based on the new user_code inserted and matching usercode_c.</p>
  
  <p>And then will insert into 2 fields in the abc12_employees_a123_employees_punches_1_c table (abc12_employees_a123_employees_punches_1abc12_employees_ida (varchar(36)) and abc12_empl4863punches_idb (varchar(36))</p>
  
  <p>I tried this so far:</p>
  
  <pre><code>CREATE TRIGGER relate_punch_to_employee AFTER INSERT on a123_employees_punches
  FOR EACH ROW
  BEGIN
      SELECT id_c INTO @id FROM a123_employees_punches_cstm WHERE usercode_c = NEW. user_code ;
  
      INSERT into abc12_employees_a123_employees_punches_1_c (abc12_employees_a123_employees_punches_1abc12_employees_ida, abc12_empl4863punches_idb) VALUES (@id, NEW.id);
  END$$
  </code></pre>
  
  <p>but it gives me the following error:</p>
  
  <blockquote>
    <p>1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4</p>
  </blockquote>