查询表中的行并显示特定行(如果它们存在于另一个中)

时间:2013-11-22 00:07:15

标签: php mysql sql

我有两个不同的表

enter image description here

enter image description here

首先我使用了$ timeapp_id,这是登录用户的当前user_id。

我想只在user_info中显示$ timeapp_id出现在timesheet_approver_1中的行

因此,如果我的$ timeapp_id目前为8,那么我应该只从user_info中显示三行。

我设法使用此代码使用此部分:


$data2b = "select * from user_info where timesheet_approver_1 = $timeapp_id";

$result2b = mysql_query($data2b);
while ($row2b = mysql_fetch_assoc($result2b)) {

$timesheet_approver_1 = $row2b['timesheet_approver_1'];
$timesheet_approver_2 = $row2b['timesheet_approver_2'];
$user_id2 = $row2b['user_id'];

//echo "( $timesheet_approver_1 / ";
//echo "$timesheet_approver_2 )";
echo "(($user_id2))";

因此,运行此代码会从表user_info中获取三行,其中user_id为5,7和34。

这就是我现在被困住的地方。我想要做的是只显示time_data中的行,如果time_data中的user_id与user_info中的user_id匹配

如果这样做,我应该只看到time_data中user_id为8的行,所以只显示一行。

我该怎么做?

由于

大卫

2 个答案:

答案 0 :(得分:2)

对于变量$ data2b,您可以首先使用表time_data ON user_info.user_id = time_data.user_id来加入表user_info,然后您可以将参数WHERE给予user_info.timesheet_approver_1 = $ timeapp_id

答案 1 :(得分:0)

我所做的是通过数组传递数据。我已经让代码接近工作但是致命错误,所以没有通过数组工作这是我在本网站上使用的正确工作代码:Barmar:

PHP array displaying last row values from table

$array = array();

while ($row2b = mysql_fetch_assoc($result2b)) {

    $timesheet_approver_1 = $row2b['timesheet_approver_1'];
    $timesheet_approver_2 = $row2b['timesheet_approver_2'];

    $array[] = $row2b['user_id'];
}

$withComma = implode(", ", $array);
相关问题