显示所有孩子的最爱

时间:2015-08-21 05:39:04

标签: php mysql

我有这两张桌子..

表1"孩子"

enter image description here

表2"收藏夹"

enter image description here

我想要的是在表格中显示它..

enter image description here

我该怎么做?

这是我试过的代码     

        $result = mysql_query("SELECT kids.*, favorites.* from kids INNER JOIN favorites ON kids.kids=favorites.kids");
       $number='1';if (mysql_num_rows($result) > 0) {
      while($row = mysql_fetch_array($result))
        {

          $idnumber=$row['kids'];

          ?>

          <td class="centered"><?php echo $number;?></td>

          <td class="centered hidden"><?php echo $idnumber;?></td>

          <td class="centered hidden"><?php echo $row['favorites'];?></td>

          <?php



      }
      ++$number;

    }

  ?>

`

4 个答案:

答案 0 :(得分:2)

尝试:

SELECT a.int,a.kids,b.kids,GROUP_CONCAT(b.favourite) from kids a, favourites b where a.kids = b.kids

这应该有效

答案 1 :(得分:2)

尝试:

SELECT
  kids.int as '#', kids.kids as 'Kids',
  GROUP_CONCAT(favorites.favr) as 'Favourites'
FROM kids
  JOIN favorites on kids.kids = favorites.kids
GROUP BY favorites.kids

答案 2 :(得分:0)

SELECT kids.kids, favorites.favorites FROM kids INNER JOIN favorites ON kids.kids=favorites.kids;

是一个示例,但我建议您更多地了解SQL连接。

答案 3 :(得分:0)

这就是你想要的 -

SELECT a.int as int, a.kids as Kids, GROUP_CONCAT(b.favourite separator ',') as Favourites 
FROM kids a, favourites b WHERE a.kids = b.kids