>백엔드 개발 >PHP 튜토리얼 >数据库中已知一个表数据查询另一个表数据的问题

数据库中已知一个表数据查询另一个表数据的问题

WBOY
WBOY원래의
2016-06-23 14:17:101083검색

从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);	?>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.