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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据