MySQL: Join (2 tables) versus single queries (1 table)

In PHP , I have an array of 11 people where only the identifier of each person is provided :

$persons = array(1, 3, 39, 72, 17, 20, 102, 99, 77, 2, 982);

My MySQL database has a table containing detailed information about each person:

TABLE personInfo
 - ID (integer)
 - name (string)
 - birth date (timestamp)
 - weight (decimal)
 - ...

Problem:

So now I want to choose a suitable name for each ID in the PHP array . I can only imagine two solutions for this:

1. for the cycle:

foreach ($persons as $person) {
    $result = mysql_query("SELECT name FROM personInfo WHERE id = ".$person);
}

2. logical operator OR

$result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");

Both solutions are slow , right?

But if I had another MySQL table containing the identifiers of the PHP array ...

TABLE ids
 - ID (integer)

... , MySQL-, ?

$result = mysql_query("SELECT a.ID, b.name FROM ids AS a JOIN personInfo AS b ON a.ID = b.ID");

:

? : ? MySQL , ? ? ( , PHP)?

+5
2

( ), .

:

+2

, , .

 $result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");

,

$result = mysql_query("SELECT name FROM personInfo WHERE id IN (1,2,3,4....) )
+2

All Articles