Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect a value of one mySQL table to a value of another table?

Tags:

php

mysql

I have two tables in my mySQL database:

table "animals":

|   animal   |    name     | 
|:-----------|------------:|
|    cat     |     Tom     | 
|    dog     |             |   

table "orders":

|     id     |   animal    | 
|:-----------|------------:|
|      1     |     cat     | 
|      2     |     dog     |  

At first I select from the table "orders" the following data:

    <?php 
    $pdo = Database::connect();
    $sql = 'SELECT * FROM orders ORDER BY id ASC';
    foreach ($pdo->query($sql) as $row) {

    echo ('<td>a:'.$row['id'].'</td>');     
    echo ('<td>b:'.$row['animal'].'</td>'); 
    echo ('<td>c:'.$row['animal'].'</td>');         

    }
    Database::disconnect();
    ?>

Now I want to check if in my mySQL table "animal" the animal has a name. If yes print at position b the name. If there is no name print the animal:

|      a:1     |     b:Tom     |      c:cat     | 
|      a:2     |     b:dog     |      c:dog     | 

Thank you for your answers! I tried to work now with the answer of Jayo2k. I need to do a little change in my question, I found out I did a little mistake. So here I try to describe what I need as specific as possible:

table "animals":

|   name     |   animal    | 
|:-----------|------------:|
|    Tom     |     cat     | 
|   Jerry    |     dog     |   
|   Alfred   |    duck     |  
|    Sam     |             | 
|   Donald   |             |  

table "orders":

|     id     |   animal    | 
|:-----------|------------:|
|      1     |     cat     | 
|      2     |     dog     |
|      3     |     duck    |
|      4     |     frog    |
|      5     |     pig     |

With the following code from Jayo2k...

    <?php 
    $pdo = Database::connect();
    $sql = "SELECT * FROM animals, orders WHERE orders.animal = animals.animal";
    foreach ($pdo->query($sql) as $row) {

    echo '<tr> ';
    echo('<td>a:'.$row['id'].' </td>');
    echo('<td>a:'.$row['animal'].' </td>');
    echo('<td>b:'.$row['name'].' </td>');
    echo '</tr> ';

    }
    Database::disconnect();
    ?>      

... I get this result:

|      a:1     |     b:cat     |      c:Tom     | 
|      a:2     |     b:dog     |      c:Jerry   |
|      a:3     |     b:duck    |      c:Alfred  |

But what I need is:

|      a:1     |     b:cat     |      c:Tom     | 
|      a:2     |     b:dog     |      c:Jerry   |
|      a:3     |     b:duck    |      c:Alfred  |
|      a:4     |     b:frog    |      c:frog    |
|      a:5     |     b:pig     |      c:pig     |
like image 980
peace_love Avatar asked Jul 14 '15 16:07

peace_love


People also ask

How get values from another table in SQL?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.

How do I link to another table in SQL?

The join is done by the JOIN operator. In the FROM clause, the name of the first table ( product ) is followed by a JOIN keyword then by the name of the second table ( category ). This is then followed by the keyword ON and by the condition for joining the rows from the different tables.


1 Answers

You can use LEFT JOIN, and use the IF condition to check the value is not empty, along with IFNULL, that will make null values in columns to blank.

SELECT O.id, IF(IFNULL(A.name, '') = '', A.animal, A.name) name,  A.animal
FROM orders O
LEFT JOIN animals A 
    ON O.animal = A.animal
ORDER BY O.id DESC
like image 177
kamal pal Avatar answered Oct 31 '22 16:10

kamal pal