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条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿