Heim >Backend-Entwicklung >PHP-Tutorial >数据库中已知一个表数据查询另一个表数据的问题
从tb_xmpcps 可以找到用户名$myrow2['xmpcps_user'],然后再通过用户名去tb_user中找到姓名,两个表嵌套查询还真是第一次做,劳大家指点指点!为什么中说$query2有错误?
<?php //查找评分表中相应批次评审状态 $query2=mssql_query("select * from tb_xmpcps where xmpcps_pcid='$xmpc_id'"); $x=1; while($myrow2=mssql_fetch_array($query2)){ ?> <tr> <td height="22" align="center" bgcolor="#FFFFFF"><span class="STYLE1"><?php echo $x;?>.</span></td> <?php //找到与用户名匹配的评委姓名 $query3=mssql_query("select * from tb_user where online_user={$myrow2['xmpcps_user']} limit 1"); $myrow3=mssql_fetch_array($query3); ?>
试试连接表
SELECT B.* FROM tb_xmpcps AS A LEFT OUVER JOIN tb_user AS B ON A.xmpcps_user=B.online_user WHERE A.xmpcps_pcid='$xmpc_id'
$query2=mssql_query("SELECT B.* FROM tb_xmpcps AS A LEFT OUTER JOIN tb_user AS B ON A.xmpcps_user=B.online_user WHERE A.xmpcps_pcid='{$xmpc_id}'");
还是报错啊?为什么?说$query2有错误?劳驾再帮我看看!
<?php //查找评分表中相应批次评审状态 $query2=mssql_query("SELECT B.* FROM tb_xmpcps AS A LEFT OUTER JOIN tb_user AS B ON A.xmpcps"); $x=1; while($myrow2=mssql_fetch_array($query2)){ ?> <tr> <td height="22" align="center" bgcolor="#FFFFFF"><span class="STYLE1"><?php echo $x;?>.</span></td> <td height="22" align="center" bgcolor="#FFFFFF" class="STYLE1"><?php echo $myrow2[online_name];?></td> <td align="center" bgcolor="#FFFFFF" class="STYLE1"><?php echo $myrow2[xmpcps_user];?></td> <td align="center" bgcolor="#FFFFFF"><span class="STYLE1"><?php echo $myrow2[xmpcps_state];?></span></td> </tr> <?php $x++; } ?>
echo mssql_get_last_message();
看看都有什么
我最终还是用两个嵌套查询解决了,这样就可以了,还是感谢大家的帮助!
<?php //查找评分表中相应批次评审状态 $query2=mssql_query("select * from tb_xmpcps where xmpcps_pcid='$xmpc_id'"); $x=1; while($myrow2=mssql_fetch_assoc($query2)){ ?> <tr class="STYLE1"> <td height="22" align="center" bgcolor="#FFFFFF"><span class="STYLE1"><?php echo $x;?>.</span></td> <?php //找到与用户名匹配的评委姓名 $query3=mssql_query("select * from tb_user where online_user='$myrow2[xmpcps_user]'"); $myrow3=mssql_fetch_assoc($query3); ?>