我还在学习SQL查询。我试过在网上搜索,但我甚至不知道我应该搜索什么条款,所以我正在找人指出我正确的方向。我有两张桌子 - 约会和登录。我正在尝试使用concat函数来获取“apptFor”和“addedBy”的名字和姓氏。我需要单独查询吗?它似乎应该可以在一个查询中完成。
<table border="1">
<tbody>
<tr>
<td width="140" align="center"><strong>Appt Date/Time</strong></td>
<td width="100" align="center"><strong>Appt For</strong></td>
<td width="300" align="center"><strong>Appointment</strong></td>
<td width="100" align="center"><strong>Added By</strong></td>
<td width="100" align="center"><strong>Date Added</strong></td>
</tr>
<?php
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy,a.added,concat(u.firstName,' ',u.lastName) as appAddedBy FROM appointments a ";
$query .=" INNER JOIN logins u on a.addedBy=u.userid WHERE a.userID='$uid' order by appDate asc";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['appDate']; ?></td>
<td><?php echo $row['apptFor']; ?></td>
<td><?php echo $row['appNote']; ?></td>
<td><?php echo $row['appAddedBy']; ?></td>
<td><?php echo $row['added']; ?></td>
</tr>
<?php }} ?>
</tbody>
</table>
答案 0 :(得分:2)
如果你需要两个名字,你应该加入两次登录
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy
,a.added,concat(u1.firstName,' ',u1.lastName) as appAddedBy
,concat(u2.firstName,' ',u2.lastName) as appApptFor
FROM appointments a ";
$query .=" INNER JOIN logins u1 on a.addedBy=u1.userid";
$query .=" INNER JOIN logins u2 on a.apptFor=u2.userid
WHERE a.userID='$uid' ";
order by appDate asc";
$result = mysqli_query($con, $query);