title: Execute > 1 SQL statement in single prepared statement
body: <p>Alright so I'll try to explain it as simple as possible; consider that I have two database tables (MySQL Server / MariaDB, database-related tasks coded in procedural style in PHP using prepared statements):</p>
  <ol>
  <li><p>in one, I have a column of datatype JSON, whose content corresponds to sth like <code>{name1:info,name2:info}</code></p>
  </li>
  <li><p>In another one, I have simple non-json records, having a structure like:</p>
  </li>
  </ol>
  <pre><code>name  | status
  ------+--------
  name1 | statusX
  ------+--------
  name2 | statusY
  </code></pre>
  <p>My Goal: I need to retrieve the name2 from table 1), but I also need to retrieve the status of the person having that same name (which in this case is statusY). Note that, for the retrieval of name2, I cannot rely on indexes of the json object (name2 may be the first key of the json object).</p>
  <p>How I would do it so far:
  A) Get the name2 from table 1) in a first query, sanitize it, and
  B) use it in the second query which then correctly retrieves the statusY</p>
  <p>Both statements A) and B) are parametrized prepared sql statements, triggered by an AJAX Call at a regular interval (AJAX Polling).</p>
  <p>Given that these database queries are thus executed frequently, I want them to be executed as fast as possible, and thus ideally reduce my two queries above to a single one. My problem: I need the result of statement A) to execute statement B), so I cannot summarize the two queries into a single prepared statement, as prepared statements cannot contain multiple sql statements. The best solution to reach what I want is create a stored procedure like:</p>
  <p><code>SET @name = SELECT ..... FROM table_1; SELECT .... FROM table_2;</code></p>
  <p>And then execute it as parametrized prepared statement; is that correct?
  I'm not at all experienced with stored procedures in MySQL Server, didn't really need them yet, but they seem to be the only solution if you want to wrap &gt; 1 sql statements into a single prepared statement. Is this assumption, and thus the conclusion that I gotta create a stored procedure to reach what I want, correct?</p>
  <p>IMPORTANT NOTE: I don't know the name I need to query. From the two names of the json column of table 1), I only know the other name. In other words, I have one name of a person X, and I want to get the status of all the persons which have been associated with that person X in table 1), while the status of each person is listed in table 2), to avoid to have duplicate status storage in the DB. ATM, I retrieve the other names of each relation record from DB 1) by using a conditional statement saying sth like</p>
  <p><strong>UPDATE</strong></p>
  <p>See added answer below, got it working.</p>