0

I would like to have e single query to get column value without "while"... is possible? I have two table like this... Owners:

+----+-------+---------+
| id | names | surname |
+------------+---------+
| 1  | John  | Red     |
| 2  | Mark  | Green   |
| 3  | Frank | Yellow  |
| ...| ...   |...      |
+------------+---------+

Animals

+----+--------+--------+
| id |idOwner | animal |
+-------------+--------+
| 1  | 1      | Cat    |
| 2  | 2      | Bird   |
| 3  | 1      | Dog    |
| ...| ...    |...     |
+-------------+--------+

Now I have this code:

$query='SELECT * FROM Owners WHERE id=1';
$con=new mysqli($dbhostname, $dbusername, $dbpassword, $dbname);
$result=$con->query($query);
$arr=array();
while ($own = $result->fetch_array(MYSQLI_ASSOC)) {
    $qry='SELECT animal FROM Animals WHERE idOwner=own["id"]';
    $rslt=$con->query($qry);
    $i=1;
    while ($aml = $rslt->fetch_array(MYSQLI_ASSOC)) {
        $txt='animal'.$i;
        $own['animal'.$i]=$aml['animal'];
        $i++;
    }
    $arr[]=$own;
}
$con->close();

I would like to have the same result but with a single query because now the process is too late... is possible?

1
  • SELECT * FROM owners o INNER JOIN animals a ON a.idOwner = o.id WHERE o.id = 1 Commented Jan 26, 2016 at 15:45

2 Answers 2

1

Yes, all you have to do is use a JOIN:

SELECT owners.*, animals.animal
FROM owners
LEFT JOIN animals
ON owners.id = animals.idOwner

Now you can display all of the owners and their animals in one loop or limit the query to one owner by using a WHERE clause. For instance, adding this clause:

WHERE owners.id = 1

Will return 2 rows from what you're showing in your tables.


OP mentions in comments that he desires one row per owner. To get a single row for each owner you will have to employ a sub-query and a GROUP_CONCAT (totally untested):

SELECT owners.*, sub-animals.animal
FROM owners
LEFT JOIN (SELECT GROUP_CONCAT(animal) AS animal
          FROM animals 
          GROUP BY animals.idOwner) AS sub-animals 
ON owners.id = animals.idOwner
Sign up to request clarification or add additional context in comments.

7 Comments

thanks... but I would like to have only 1 rows... suggest?
One row with all of the animals in the row @FrancescoG.? If so, you will need a sub-query for the animals part. Your original question did not state the desired outcome, so maybe this is a different question?
Yes one row for owner... I would like to have for example... Jhon, Red,..., cat, dog
Can you setup a SQLFiddle with your data? @FrancescoG.
yes... but I have done... thanks so much... There is an error in the query, the correct query is: SELECT owners.*, sub-animals.animal FROM owners LEFT JOIN (SELECT idOwner, GROUP_CONCAT(animal) AS animal FROM animals GROUP BY animals.idOwner) AS sub-animals ON owners.id = sub-animals.idOwner
|
1

Yes, you can use JOIN for one single query as:

SELECT o.*,a.animal FROM owners o 
INNER JOIN animals a ON a.idOwner = o.id 

If you want to add WHERE id = 1 clause for getting specific user data than you can use this query as:

SELECT o.*,a.animal FROM owners o 
INNER JOIN animals a ON a.idOwner = o.id
WHERE o.id = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.