如何在MySQL表上使用内连接?

时间:2015-07-09 09:14:09

标签: php mysql xampp inner-join

你好我有这样的表:

员工

EmployeeID  EmployeeName DepartementName
1234        Seulong      Accounting
1235        Jokwon       Accounting
1236        Jinwoon      IT
1237        Changmin     IT
1238        Junho        IT
1239        Taecyeon     IT

Shiftsceduling

(d代表日期,例子d01 =日期1,d02 =日期2等)

employeeID_shift month  d01 d02 d03 d04 d05 d06 d07 d08 d09 d10
1234             7      1   1   2   3   0   1   1   2   1   2
1235             7      1   2   1   2   0   1   2   3   0   1
1236             7      1   2   3   0   2   1   1   1   1   1
1237             7      1   3   1   1   1   0   1   1   0   1
1238             7      0   2   1   3   2   1   1   2   1   1
1239             7      1   1   1   1   1   1   0   0   2   1

ShiftCode

idshift start       end         information
0       00:00:00    00:00:00    OFF
1       08:00:00    16:00:00    8am - 16pm
2       09:00:00    18:00:00    9am - 18pm
3       16:00:00    04:00:00    20pm - 04am

PoliceNumber    EmployeeID_car  
J 0115 JYP      1234    
J 0634 JYP      1235    
J 1227 JYP      1236    
J 0430 JYP      1237
J 0125 JYP      1238    
J 0211 JYP      1239

我有一个功能可以爆炸今天的日期:

$date=date("Y-m-d");

 $a = $date;
 {
 $b = explode('-', $a);

 $year = $b[0];
 $bulan = $b[1];
 $date2 = $b[2];
 }

 echo $date2;

我想搜索7月2日09:00 - 17:00可用或工作的IT部门的所有员工ID,并显示他们使用的汽车。我已经使用了这段代码,并且与继续搜索他们的班次时间表相混淆:

    SELECT Employee.* , Shiftsceduling.*, car.*, ShiftCode.*
    FROM Employee
    INNER JOIN Shiftsceduling ON Employee.EmployeeID = Shiftsceduling.employeeID_shift
    INNER JOIN car ON Employee.EmployeeID = car.EmployeeID_car
    WHERE Employee.DepartementName = 'IT' AND d'.$date2.' = '2'
    AND ShiftCode.start <=  '$starttime' AND ShiftCode.end >=  '$endtime'

但它不起作用,你知道问题出在哪里吗?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我认为主要问题是你没有加入Shiftcode表。但是,通过两次检查时间段,你似乎也会让自己感到困惑。

您似乎在WHERE子句中检查时间段实际时间,检查开始和结束。但是你也检查Shiftsceduling的d01 / d02 / etc字段是2(这是09:00到18:00的时间段)。

未经测试,但我认为你想要这样的东西: -

SELECT Employee.* , 
        Shiftsceduling.*, 
        car.*, 
        ShiftCode.*
FROM Employee
INNER JOIN Shiftsceduling ON Employee.EmployeeID = Shiftsceduling.employeeID_shift
INNER JOIN car ON Employee.EmployeeID = car.EmployeeID_car
INNER JOIN ShiftCode ON ShiftCode.idshift = Shiftsceduling.d'.$date2.'
WHERE Employee.DepartementName = 'IT'
AND ShiftCode.start <=  '$starttime' 
AND ShiftCode.end >=  '$endtime'