如何做高级sql表比较

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. Graph image

The names you can see in the image are the main problem, those names are collected initially from the First Table 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".

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.

The problem comes when I edit any field on the first table because the information is not displayed in any order.

Summing up, I want to be able to edit the information on the first table without the graph messing up.

The queries (php) are:

$sql1 = "SELECT porcentaje1,idopcion1,porcentaje2
 ,idopcion2,porcentaje3,idopcion3,porcentaje4,idopcion4 
 FROM 3eso1";
$rs=$mysqli->query($sql1);

$opcion1id = $rows[0]["idopcion1"];
$opcion2id = $rows[0]["idopcion2"];
$opcion3id = $rows[0]["idopcion3"];
$opcion4id = $rows[0]["idopcion4"];

$stmt = $mysqli->prepare("SELECT nombre 
 FROM platos 
 where menuid=? OR menuid=? OR menuid=? OR menuid=?");
$stmt->bind_param('iiii', intval($opcion1id), intval($opcion2id)
  , intval($opcion3id), intval($opcion4id));
$stmt->execute();

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.

You need only one query, joining two tables, something like this:

SELECT
    n1.nombre as nombre1, 3eso1.porcentaje1,
    n2.nombre as nombre2, 3eso1.porcentaje2,
    n3.nombre as nombre3, 3eso1.porcentaje3,
    n4.nombre as nombre4, 3eso1.porcentaje4
FROM 3eso1 
    LEFT JOIN platos n1 ON n1.menuid=3eso1.idopcion1
    LEFT JOIN platos n2 ON n2.menuid=3eso1.idopcion2
    LEFT JOIN platos n3 ON n3.menuid=3eso1.idopcion3
    LEFT JOIN platos n4 ON n4.menuid=3eso1.idopcion4

If I understand you correctly, your dynamic chart shows the data in the order it is returned by the query; you're lucky it has been working at all, because SQL does not guarantee any order in the results unless you explicitly sort your data. If so, continue editing your data however you want, but edit the query behind the graph to include an ORDER BY clause. It is probably enough to just order by ID.

If that's not what you are doing at all, please edit your question and add all code related to the queries that generate the pie chart. (The graphing code itself is not important.)