dqv2743 2016-04-06 05:14
浏览 380
已采纳

使用外键查看关系表中的数据

Trying to fetch data from users table by user_id which is the foreign key in vehicle table. The relation is working properly like as when I insert data in the vehicle table then user_id also inserting as expected, but the problem is when I want to fetch data from users table using user_id.

I used this SQL query for viewing data

 <?php 
  $qry = mysql_query("select vehicle. *, users.zip AS zip from vehicle  JOIN users ORDER BY id DESC ");
  while($data = mysql_fetch_array($qry))

 {?>


  <tr role="row" class="even">

      <td style="display:none;"><?php echo $data['id'];?></td>  

      <td class="sorting_1">  <?php $datess = $data['create_date'];
         $old_date_timestamp = strtotime($datess);
         echo date('m-d-Y', $old_date_timestamp);?>

      </td>
      <td><?php echo $data['bid_type'];?></td>

      <td><?php echo $data['zip'];?></td>
      <td>
        <a href="bid-detail.php?id=<?php echo base64_encode($data['id']);?>" class="btn btn-success btn-small">$ Make Offer</a>
      </td>

   </tr>

<?php
 }
 ?>

This query displaying data, but it's repeating one again and again.

Anyone can help me please to solve the issue or any other easy solution for this.

  • 写回答

2条回答 默认 最新

  • duanfan9859 2016-04-06 05:48
    关注

    Issue in your query.

    You have to add WHERE clause in your query which will match your PK of parent table with FK of child table.

    Try This:

    select v.*, u.zip AS zip from vehicle as v,users as u WHERE v.user_id=u.user_id ORDER BY v.id DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功