0

我正在运行这个运行良好的语句....

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

我的问题是列技能存储来自表机构_技能的值,该表有一个包含技能名称的列。

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

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

<?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>
4

1 回答 1

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

如果您使用 aleft outer join而不是默认值inner join,您会得到表skill中不存在的偶数skills

简化的 SQL Fiddle 演示

于 2013-02-17T00:27:41.003 回答