0

我有一张表用于存储本周的飞机检查时间表,它被称为aircraft_sched. 还有另外两个相关的,一个是aircraft_sched_options我需要对JOIN第一个表调用的,最后一个表是调用的aircraft

aircraft_sched

column 1: AC_Reg (VARCHAR)(10),(PK),(FK -> `aircraft` PK)
column 2: Sched_Day1 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 3: Sched_Day2 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 4: Sched_Day3 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 5: Sched_Day4 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 6: Sched_Day5 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 7: Sched_Day6 (INT)(1),(FK -> `aircraft_sched_options` PK)
column 8: Sched_Day7 (INT)(1),(FK -> `aircraft_sched_options` PK)

aircraft_sched_options

column 1: SchedOpt_ID (INT)(1),(PK)
column 2: SchedOpt_Name (VARCHAR)(10)
column 3: SchedOpt_Color (VARCHAR)(7),

aircraft

column 1: AC_Reg (VARCHAR)(10),(PK)
column 2: AC_SN  (VARCHAR)(6)
column 3: AC_Year (VARCHAR)(4)

当将新飞机添加到系统中时,我拥有它,因此它也将其添加到aircraft_sched表中。我不认为这是正确的方法,但现在就是这样。因此,aircraft_sched表格始终填充有 ,AC_Reg并且Sched_DayX单元格是与所选计划类型相对应的0forNULL或数字。SchedOpt_ID

我面临的问题是当我尝试JOINSched_DayX列到SchedOpt_ID列时。当我只有JOIN一列时它有点工作,但如果我尝试做不止一列,那么该行就会从我的结果中消失。

这是我的“有点”工作的代码:

<?php
$sql = ("
          SELECT 
             * 
          FROM 
             aircraft_sched 
          INNER JOIN aircraft_sched_options AS aso1 
             ON aircraft_sched.Sched_Day1 = aso1.SchedOpt_ID
");

if(!$result_sql = $mysqli->query($sql))
{
    echo QueryCheck("getting the aircraft schedule ","from the aircraft sched",$mysqli) . "Error No: " .$mysqli->errno;
}

while($ACSched = $result_sql->fetch_assoc())
{
    echo "<tr>";
    echo "<td class=\"ACSched_Reg\">" . $ACSched['AC_Reg'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "<td align=\"center\" style=\"background:" . $ACSched['SchedOpt_Color'] . ";\">" . $ACSched['SchedOpt_Name'] . "</td>";
    echo "</tr>";
}
?>

当我说它“有点”有效时,我的意思是它实际上显示了一些结果。它不起作用的原因是因为它在每个单元格中显示相同的结果,即使只有一天分配了计划类型。

当我将第二个添加JOIN到查询中时,如下所示:

$sql = ("
          SELECT 
            * 
          FROM 
            aircraft_sched 
          INNER JOIN aircraft_sched_options AS aso1 
            ON aircraft_sched.Sched_Day1 = aso1.SchedOpt_ID
          INNER JOIN aircraft_sched_options AS aso2 
            ON aircraft_sched.Sched_Day2 = aso2.SchedOpt_ID
 ");

...然后它只是不返回任何有记录的行。

我一直在阅读JOINs 以及它们是如何工作的,我能想到的解决问题的唯一方法是为每个表设置一个单独的表,AC_Reg以便将一周中的每一天设置为一UNIQUE列,但我不相信这是完成任务的最佳方式。

编辑:

以下是一些屏幕截图,以提供更好的视觉效果。

aircraft_sched

飞机计划

aircraft_sched_options

plane_sched_options

我的代码:

php代码

屏幕显示:

屏幕上显示的内容

4

2 回答 2

1

tbl_two 如果我正确理解了您的问题,您需要为每列加入表格Day

SELECT 
    aircraft_sched.AC_SN,
    IF(
       aso1.SchedOpt_Name IS NULL OR aso1.SchedOpt_ID = 0, 
       '-', 
       aso1.SchedOpt_Name
    ) as option1,
    IF(
       aso2.SchedOpt_Name IS NULL OR aso2.SchedOpt_ID = 0, 
       '-', 
       aso2.SchedOpt_Name
    ) as option2
FROM 
    aircraft_sched 
LEFT JOIN aircraft_sched_options AS aso1 
    ON aircraft_sched.Sched_Day1 = aso1.SchedOpt_ID
LEFT JOIN aircraft_sched_options AS aso2 
    ON aircraft_sched.Sched_Day2 = aso2.SchedOpt_ID

....

编辑:我已经更新了查询并使用LEFT JOIN而不是INNER JOIN获取选项,因为正如您所说,有些可能是NULL

更新:删除连接aircraft并添加检查选项是否为空或 id 为 0-将显示

于 2013-04-17T11:36:19.223 回答
0

按照查询的逻辑,只有在 tbl_one 中具有 Day1 == Day2 == Day3 == ... == Day7 的行并且该值也等于 tbl_two.Day_ID 应该显示出来,这有帮助吗?

SELECT * FROM tbl_one INNER JOIN tbl_two
    ON tbl_one.Day1 = tbl_two.Day_ID 
WHERE 
      tbl_one.Day1 = tbl_one.Day2
  AND tbl_one.Day1 = tbl_one.Day3
  AND tbl_one.Day1 = tbl_one.Day4
  AND tbl_one.Day1 = tbl_one.Day5
  AND tbl_one.Day1 = tbl_one.Day6
  AND tbl_one.Day1 = tbl_one.Day7
于 2013-04-17T09:58:27.527 回答