How to use the values of an array obtained from a MySQL query to perform another MySQL query?

1

I am using CodeIgniter and I am trying to do it in the following way:

$query = $this->db->query("SELECT Tabla1.id 
                                From Tabla1 INNER JOIN Tabla2 
                                ON Tabla1.id = Tabla2.id");

if($query->num_rows()>0){
            foreach ($query2->result_array() as $row){
            echo $row['id']."<br>";
            }

So far, if I can get a list with all the ids in Table1 but I need to compare each one of those id in the next query, the problem is that I do not know how to save them.

SELECT Tabla1.campo1, Tabla1.campo2, Tabla1.campo3
                    From Tabla1, Tabla2, Tabla3
                    WHERE Tabla1.campo2= Tabla2.campo2 
                    AND Tabla2.campo3 = Tabla3.campo3 
                    AND Tabla2.campo3 = $row['id'] 
    
asked by El Cóndor 09.08.2016 в 15:21
source

1 answer

1

I really do not know what madness you want, but I hope this gives you an idea of how to solve the issue with a single query.

SELECT campos-requeridos
  FROM (
    SELECT Tabla1.id 
    FROM  Tabla1 
    INNER JOIN Tabla2 
    ON Tabla1.id = Tabla2.id
  ) AS SUB1
 INNER JOIN Tabla2 on Tabla1.campo2 = Tabla2.campo3
 INNER JOIN Tabla3 on Tabla3.campo2 = Tabla2.campo3
 WHERE tabla2.campo3 = SUB1.id

I stay tuned.

    
answered by 09.08.2016 в 16:59