我如何正确地“加入”这些表格?

时间:2013-04-17 09:44:09

标签: php mysql join

我有一张表用于存储本周的飞机检查时间表,称为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_RegSched_DayX单元格为0 NULLSchedOpt_ID对应的数字JOIN选择的计划类型。

我遇到的问题是当我尝试将Sched_DayXSchedOpt_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 "); 添加到查询中时,如下所示:

JOIN

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

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

编辑:

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

aircraft_sched_options

aircraft_sched

{{1}}:

aircraft_sched_options

我的代码:

php code

屏幕显示:

what displays on the screen

2 个答案:

答案 0 :(得分: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,-将会显示

答案 1 :(得分:0)

按照查询的逻辑,只有第1行== Day2 == Day3 == ... == Day7在tbl_one中的行,并且值也等于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