显示相关表格的信息?

时间:2013-02-17 00:25:28

标签: php sql

我正在运行此声明,它可以正常工作....

$sql = "SELECT skill,SUM(quantity) as sum FROM skills
WHERE userid = $userid
GROUP BY skill";

我的问题是列技能存储表agency_skills中的值,该值包含一个包含技能名称的列。

我想弄清楚如何显示技能的名称而不是技能表中存储的值?

这是我正在使用的整个代码.....

<?php
defined( '_JEXEC' ) or die;

$db = JFactory::getDBO();
$user =& JFactory::getUser();
$userid = $user->get('id');


$sql = "SELECT skill,SUM(quantity) as sum FROM skills
WHERE userid = $userid
GROUP BY skill";

$db->setQuery($sql);

$rows = $db->loadObjectList();

?>
<style>
table, td, th
{
    border-bottom:1px solid black;
}
    {
    th
background-color:#000000
color:#FFF
    }


</style>

<table>
<tr>
<th width="195">Skill</th> <th width="195">Total Completed</th>
</tr>
</table>
<table>
<?PHP foreach ($rows as $row): ?>

<tr>

<td width="200"> <?php echo $row->skill?> </td>

<td width="190"> <?php echo $row->sum ?> </td>
</tr>
<?php endforeach ?>
</table>

1 个答案:

答案 0 :(得分:2)

您可以像这样加入该表

SELECT as.skillname,
       SUM(s.quantity) as sum 
FROM agency_skills as `as`
left outer join skills as s on s.skill = as.id
WHERE s.userid = $userid
GROUP BY as.skillname

如果您使用left outer join代替默认inner join,则skill表中不会出现skills

Simplified SQL Fiddle demo