我想显示具体要求的名称和日期。例如,要求oracle DBA在各种日期有3个提交。表'require'的字段为e_id和req_id。
第二个表'submittal'有req_id,submittal和date字段 我想根据需要在一列中显示提交,在另一列显示日期。
我尝试使用数组值显示
$sub="select a.*,b.c_name from requirement as a,client as b where a.c_id=b.c_id and e_id='2'";
$sub1=mysql_query($sub);
while($sfet=mysql_fetch_assoc($sub1))
{
echo '<tr>';
$req_id=$sfet['req_id'];
echo '<td>'.$sfet['c_name'].'</td>';
echo '<td>'.$sfet['title'].'</td>';
$was="select * from resume where req_id='$req_id'";
$was1=mysql_query($was);
while($wfet=mysql_fetch_assoc($was1))
{
$array[] = $wfet;
}
echo '<td>';
echo $array[]['username'];
echo '</td>';
echo '<td>';
echo $array[]['date'];
echo '</td>';
echo '</tr>';
}
答案 0 :(得分:0)
按如下方式更改您的代码:
while($sfet=mysql_fetch_assoc($sub1))
{
echo '<tr>';
$req_id=$sfet['req_id'];
echo '<td>'.$sfet['c_name'].'</td>';
echo '<td>'.$sfet['title'].'</td>';
$was="select * from resume where req_id='$req_id'";
$was1=mysql_query($was);
$username = $date = [];
while($wfet=mysql_fetch_assoc($was1))
{
$username[] = $wfet['username'];
$date[] = $wfet['date'];
}
echo '<td>';
echo implode('<br>',$username);
echo '</td>';
echo '<td>';
echo implode('<br>',$date);
echo '</td>';
echo '</tr>';
}
修改强>
更好的解决方案是使用GROUP_CONCAT
,如下所示:
$sub = "SELECT a.*, b.c_name,
GROUP_CONCAT(c.username ORDER BY c.date SEPARATOR '<br>') AS g_username,
GROUP_CONCAT(c.date ORDER BY c.date SEPARATOR '<br>') AS g_date
FROM requirement as a
JOIN client as b ON a.c_id=b.c_id
JOIN resume c ON a.req_id=c.req_id GROUP BY a.req_id";
$sub1=mysql_query($sub);
while($sfet=mysql_fetch_assoc($sub1))
{
echo '<tr>';
$req_id=$sfet['req_id'];
echo '<td>'.$sfet['c_name'].'</td>';
echo '<td>'.$sfet['title'].'</td>';
echo '<td>'.$sfet['g_username'].'</td>';
echo '<td>'.$sfet['g_date'].'</td>';
echo '</tr>';
}