php + mysql 查询结果作为条件再查另一个表,如何弄?

2024-11-09 10:45:10
推荐回答(5个)
回答(1):

方法一:很简单,再查询一次就好了,代码如下


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>






方法二: 用联合查询,直接查出两张表的数据

$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)) {

?>






回答(2):

你这里是不能用do while的,结果都没返备遍历出来,不可能有值的




$sql="select * from 表B where userid=".$row_Recordset1['userid'];
$Recordset2=mysql_query($sql);
$row_Recordset1 = mysql_fetch_assoc($Recordset2)

?>
<漏脊毁td>


回答(3):

你用备激弊联合查询
$sql="select * from 表仿族A,表B where 表A.userid=表B.userid"铅漏;
$re=mysql_query($sql);
while($row=mysql_fetch_assoc($re)){
$class[]=$row;

};
一条sql语句就搞定了

回答(4):

联合查询:$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 "猛袜";
echo "<岁和td>{$row['name']}{$row['mobile']}"

echo "";

}

回答(5):

你把表A的数据查出来,然根据id update进去表B不就可以了么