title: How to do advanced sql table comparing
body: <p>I'm building a website that shows a dynamic chart, the info from the chart is collected from a database, the process to get the information is divided in two steps, the first sql step is get the percentages and the string id shown with the percentage.
  <a href="https://i.stack.imgur.com/r1PeK.png" rel="nofollow noreferrer">Graph image</a></p>
  
  <p>The names you can see in the image are the main problem, those names are collected initially from the <a href="https://i.stack.imgur.com/baMqI.png" rel="nofollow noreferrer">First Table</a> by getting the id of the string, for example the string "Carne Estofada" holding the 45% of the graph is symbolized in the first table with the id "1".</p>
  
  <p>In order to get the strings and not show a the string id I make a second query from the Second Table (two columns: id and name for each id) in which i get the strings out of the ids. Then I display it all.</p>
  
  <p>The problem comes when I edit any field on the first table because the information is not displayed in any order. </p>
  
  <p>Summing up, I want to be able to edit the information on the first table without the graph messing up.</p>
  
  <p>The queries (php) are:</p>
  
  <pre><code>$sql1 = "SELECT porcentaje1,idopcion1,porcentaje2
   ,idopcion2,porcentaje3,idopcion3,porcentaje4,idopcion4 
   FROM 3eso1";
  $rs=$mysqli-&gt;query($sql1);
  
  $opcion1id = $rows[0]["idopcion1"];
  $opcion2id = $rows[0]["idopcion2"];
  $opcion3id = $rows[0]["idopcion3"];
  $opcion4id = $rows[0]["idopcion4"];
  
  $stmt = $mysqli-&gt;prepare("SELECT nombre 
   FROM platos 
   where menuid=? OR menuid=? OR menuid=? OR menuid=?");
  $stmt-&gt;bind_param('iiii', intval($opcion1id), intval($opcion2id)
    , intval($opcion3id), intval($opcion4id));
  $stmt-&gt;execute();
  </code></pre>
  
  <p>Actually I sort my data manually but I want that it automatically orders itself on the graph so that the correct percentage is shown with the correct string.</p>