<table border="0" align="center" cellpadding="0" cellspacing="0">
<? mysql_query("SET NAMES GBK");
$sql = mysql_query("SELECT * FROM characters left join departments on departments.departmentid=characters.departmentid ORDER BY salary DESC LIMIT 0,20"); while ($data = mysql_fetch_array($sql)){ ?>
<? echo $data["rmbs"]; ?>
<tr><td width="150" height="20"><strong>姓名:</strong><? echo $data[name]; ?></td>
<td width="70" height="20"><strong>薪金:</strong><? echo $data[salary]; ?></td>
<td width="70" height="20"><strong>年龄:</strong><? echo $data[reborns]; ?></td>
<td width="150" height="20"><strong>部门:</strong><? echo $data[departments.name]; ?></td>
</tr>
? }?>
</table>
----------------------------------------------------------------
有两个表,人物表characters和部门表departments,要求在网页中显示人物姓名、年龄、薪金和其所在的部门,并且以薪金从高到低显示出前20位的人物。
现在的问题是显示不出人物所在的部门名称
请高手指教,多谢!!!
$sql = mysql_query("SELECT characters.*,departments.name as depname FROM characters left join departments on departments.departmentid=characters.departmentid ORDER BY salary DESC LIMIT 0,20");
部门:</strong><? echo $data[depname]; ?>
如果还不行,那么你直接在mysql客户端执行一下
SELECT characters.*,departments.name as depname FROM characters left join departments on departments.departmentid=characters.departmentid ORDER BY salary DESC LIMIT 0,20
如果depname没有值的话,那么就说明数据库里就没有值了。你自然也取不出来。
语句改一下试试:
SELECT characters .*,departments.name as dept_name FROM characters left join departments on departments.departmentid=characters.departmentid ORDER BY salary DESC LIMIT 0,20
==============
在while循环中部门使用 $date['dept_name'];