如何将一个mySQL表的值连接到另一个表的值?

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


I have two tables in my mySQL database:

我的mySQL数据库中有两个表:

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:

现在我想检查一下我的mySQL表中的“动物”是否有动物的名字。如果是,则在位置b打印名称。如果没有名字打印动物:

|      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:

谢谢您的回答!我现在尝试使用Jayo2k的答案。我需要对我的问题做一点改动,我发现我犯了一点错误。所以在这里我尝试尽可能具体地描述我需要的东西:

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

使用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     |

2 个解决方案

#1


5  

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.

您可以使用LEFT JOIN,并使用IF条件检查值是否为空,以及IFNULL,这将使列中的空值为空。

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

#2


0  

What I do is (I am using PDO):

我所做的是(我正在使用PDO):

SELECT * FROM animal, orders WHERE orders.animal = animals.animal

It will select both animals and orders table and joint the animal row from orders with the animal row from animal.

它将选择动物和命令表,并从动物的动物行的命令中联合动物行。

you should get an array like this

你应该得到这样的数组

[0] =>
      id = 1
      name = tom
      animal = cat
[1] =>
      id = 2
      name = 
      animal = dog

Now up to you to do all the modification you want

现在由你来做你想要的所有修改

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/07/14/cff20b42d2a304b6ef34287885a7d8fc.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告