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 软件定义网络mininet和onos控制器问题
  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。