php + mysql 查询结果作为条件再查另一个表,如何弄?
表Auseridname1张三2李四3赵五表Biduseridmobile13138999999992215566667777查询表B后如下列表<?phpdo{?><tr...
表A
userid name
1 张三
2 李四
3 赵五
表B
id userid mobile
1 3 13899999999
2 2 15566667777
查询表B后如下列表
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['user_id']; ?></td>
<td><?php echo $row_Recordset1['mobile']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
如何把查询结果列表中的user_id替换成表A中的name 展开
userid name
1 张三
2 李四
3 赵五
表B
id userid mobile
1 3 13899999999
2 2 15566667777
查询表B后如下列表
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['user_id']; ?></td>
<td><?php echo $row_Recordset1['mobile']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
如何把查询结果列表中的user_id替换成表A中的name 展开
5个回答
展开全部
方法一:很简单,再查询一次就好了,代码如下
<?php
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
$sql = "select * from 表A where userid={$row_Recordset1['user_id']} limit 1";
$query = mysql_query($sql);
$user = mysql_fetch_assoc( $query );
?>
<tr>
<td><?php echo $user['name']; ?></td>
<td><?php echo $row_Recordset1['mobile']; ?></td>
</tr>
<?php } ?>
方法二: 用联合查询,直接查出两张表的数据
<?php
$sql = "select * from tableA,tableB where tableA.userid = tableB.userid limit 10";
$query = mysql_query($sql);
$user = mysql_fetch_assoc( $query );
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
?>
<tr>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['mobile']; ?></td>
</tr>
<?php } ?>
<?php
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
$sql = "select * from 表A where userid={$row_Recordset1['user_id']} limit 1";
$query = mysql_query($sql);
$user = mysql_fetch_assoc( $query );
?>
<tr>
<td><?php echo $user['name']; ?></td>
<td><?php echo $row_Recordset1['mobile']; ?></td>
</tr>
<?php } ?>
方法二: 用联合查询,直接查出两张表的数据
<?php
$sql = "select * from tableA,tableB where tableA.userid = tableB.userid limit 10";
$query = mysql_query($sql);
$user = mysql_fetch_assoc( $query );
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
?>
<tr>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['mobile']; ?></td>
</tr>
<?php } ?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这里是不能用do while的,结果都没遍历出来,不可能有值的
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr>
<td><?php echo $row_Recordset1['name']; ?></td>
<?php
$sql="select * from 表B where userid=".$row_Recordset1['userid'];
$Recordset2=mysql_query($sql);
$row_Recordset1 = mysql_fetch_assoc($Recordset2)
?>
<td><?php echo $row_Recordset2['mobile']; ?></td>
</tr>
<?php } ?>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr>
<td><?php echo $row_Recordset1['name']; ?></td>
<?php
$sql="select * from 表B where userid=".$row_Recordset1['userid'];
$Recordset2=mysql_query($sql);
$row_Recordset1 = mysql_fetch_assoc($Recordset2)
?>
<td><?php echo $row_Recordset2['mobile']; ?></td>
</tr>
<?php } ?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你用联合查询
$sql="select * from 表A,表B where 表A.userid=表B.userid";
$re=mysql_query($sql);
while($row=mysql_fetch_assoc($re)){
$class[]=$row;
};
一条sql语句就搞定了
$sql="select * from 表A,表B where 表A.userid=表B.userid";
$re=mysql_query($sql);
while($row=mysql_fetch_assoc($re)){
$class[]=$row;
};
一条sql语句就搞定了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
联合查询:$sql = "select a.name as name,b.mobile as mobile from a,b where a.userid=b.userid";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
echo "<tr>";
echo "<td>{$row['name']}</td><td>{$row['mobile']}</td>"
echo "</tr>";
}
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)) {
echo "<tr>";
echo "<td>{$row['name']}</td><td>{$row['mobile']}</td>"
echo "</tr>";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把表A的数据查出来,然根据id update进去表B不就可以了么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询