无法从MySQL中的多个表中检索记录

时间:2017-06-21 13:34:15

标签: mysql sql create-table records

我有一个像往常一样有多个表的数据库。创建数据库AMWP和其中的表的脚本如下: -

CREATE DATABASE AMWP 
COLLATE'utf8_general_ci'

CREATE TABLE `amwplist`(`Table_ID` int NOT NULL, `Category_ID` int NULL, `Work_ID_by_MoD` varchar(255) NULL, `Station_ID` int NULL, `Nomenclature` varchar(255) NULL, `Cost` decimal(10,2) NULL, `CFA_ID` int NULL, `LocalIFA_ID` int NULL, PRIMARY KEY (`Table_ID`))CHARACTER SET utf8;
CREATE TABLE `cdalist`(`CDA_ID` int NOT NULL, `CDA_Office` varchar(255) NULL, PRIMARY KEY (`CDA_ID`))CHARACTER SET utf8;
CREATE TABLE `celist`(`CE_ID` int NOT NULL, `CE_Zone` varchar(255) NULL, PRIMARY KEY (`CE_ID`))CHARACTER SET utf8;
CREATE TABLE `cfalist`(`CFA_ID` int NOT NULL, `CFA` varchar(255) NULL, PRIMARY KEY (`CFA_ID`))CHARACTER SET utf8;
CREATE TABLE `commandhqlist`(`CommandHQ_ID` int NOT NULL, `Command_Name` varchar(255) NULL, `Command_Location` varchar(255) NULL, `Command_City` varchar(255) NULL, `CommandCivilPost_Pin` int NULL, `CommandFPO_Pin` varchar(255) NULL, `CommandSTDCode` varchar(255) NULL, `CommandTeleNumber` int NULL, `CommandWksExtn` int NULL, PRIMARY KEY (`CommandHQ_ID`))CHARACTER SET utf8;
CREATE TABLE `cwelist`(`CWE_ID` int NOT NULL, `CWE_Region` varchar(255) NULL, PRIMARY KEY (`CWE_ID`))CHARACTER SET utf8;
CREATE TABLE `gelist`(`GE_ID` int NOT NULL, `GE_Division` varchar(255) NULL, PRIMARY KEY (`GE_ID`))CHARACTER SET utf8;
CREATE TABLE `initialallotment`(`Table_ID` int NOT NULL, `Work_ID_by_MoD` varchar(255) NULL, `Fund_Alloted` decimal(10,5) NULL, PRIMARY KEY (`Table_ID`))CHARACTER SET utf8;
CREATE TABLE `localifalist`(`LocalIFA_ID` int NOT NULL, `LocalIFA` varchar(255) NULL, PRIMARY KEY (`LocalIFA_ID`))CHARACTER SET utf8;
CREATE TABLE `sectorlist`(`Sector_ID` int NOT NULL, `Sector` varchar(255) NULL, PRIMARY KEY (`Sector_ID`))CHARACTER SET utf8;
CREATE TABLE `stationlist`(`Station_ID` int NULL, `Command_ID` int NULL, `Sector_ID` int NULL, `Station` varchar(255) NULL, `Location` varchar(255) NULL, `CE_ID` int NULL, `CWE_ID` int NULL, `GE_ID` int NULL, `LocalIFA_ID` int NULL, `CDA_ID` int NULL)CHARACTER SET utf8;
CREATE TABLE `workscategorylist`(`Category_ID` int NOT NULL, `Category_Type` varchar(255) NULL, PRIMARY KEY (`Category_ID`))CHARACTER SET utf8;
CREATE TABLE `worksstatus`(`Table_ID` int NOT NULL, `Work_ID_by_MoD` varchar(255) NULL, `Status_ID` int NULL, `AE_Amt` decimal(10,2) NULL, `Date` date NULL, `Remarks` varchar(255) NULL, PRIMARY KEY (`Table_ID`))CHARACTER SET utf8;
CREATE TABLE `workstatuslist`(`Status_ID` int NOT NULL, `Status_Type` varchar(255) NULL, PRIMARY KEY (`Status_ID`))CHARACTER SET utf8;

数据将改变的两个主要表是Table'amwplist',其中记录将每年填充一次,第二个表是'worksStatus',其中表'amwplist'中列出的每个工作的状态与相应的'Works_ID_by_MoD'将根据交易而变化。一旦日历年结束,这些交易将停止。

我正在尝试从所有表中检索记录并使用以下SQL显示,但结果不符合要求,并且正在显示多条记录。我尽我所能但无法达到正确的结果。所以我正在请求帮助。

SQL语句

    `SELECT
  StnDetails.Command_Name,
  StnDetails.Sector,
  amwp.workscategorylist.Category_Type,
  amwp.amwplist.Work_ID_by_MoD,
  StnDetails.Station,
  amwp.amwplist.Nomenclature,
  amwp.amwplist.Cost,
  amwp.worksstatus.AE_Amt,
  amwp.workstatuslist.Status_Type,
  amwp.worksstatus.Date,
  amwp.worksstatus.Remarks,
  StnDetails.CE_Zone,
  StnDetails.CWE_Region,
  StnDetails.GE_Division,
  StnDetails.LocalIFA,
  StnDetails.CDA_Office,
  amwp.amwplist.Fund_Alloted
FROM
  amwp.amwplist,
  (SELECT
      amwp.commandhqlist.Command_Name,
      amwp.sectorlist.Sector,
      amwp.stationlist.Station,
      amwp.celist.CE_Zone,
      amwp.cwelist.CWE_Region,
      amwp.gelist.GE_Division,
      amwp.localifalist.LocalIFA,
      amwp.cdalist.CDA_Office
    FROM
      amwp.stationlist
      LEFT JOIN amwp.commandhqlist ON amwp.commandhqlist.CommandHQ_ID = amwp.stationlist.Command_ID
      LEFT JOIN amwp.sectorlist ON amwp.sectorlist.Sector_ID = amwp.stationlist.Sector_ID
      LEFT JOIN amwp.celist ON amwp.celist.CE_ID = amwp.stationlist.CE_ID
      LEFT JOIN amwp.cwelist ON amwp.cwelist.CWE_ID = amwp.stationlist.CWE_ID
      LEFT JOIN amwp.gelist ON amwp.gelist.GE_ID = amwp.stationlist.GE_ID
      LEFT JOIN amwp.localifalist ON amwp.localifalist.LocalIFA_ID = amwp.stationlist.LocalIFA_ID
      LEFT JOIN amwp.cdalist ON amwp.cdalist.CDA_ID = amwp.stationlist.CDA_ID) AS StnDetails
  LEFT JOIN amwp.workscategorylist ON amwp.workscategorylist.Category_ID = amwp.amwplist.Category_ID
  LEFT JOIN amwp.worksstatus ON amwp.worksstatus.Work_ID_by_MoD = amwp.amwplist.Work_ID_by_MoD
  LEFT JOIN amwp.workstatuslist ON amwp.workstatuslist.Status_ID = amwp.worksstatus.Status_ID,
  RIGHT JOIN  StnDetails ON StnDetails.Station_ID = amwp.amwplist.Station_ID`

我也试过这个SQL:'选择

    StnList.Command_Name,
  StnList.Sector,
  MainList.Category_Type,
  MainList.Work_ID_by_MoD,
  StnList.Station,
  MainList.Nomenclature,
  MainList.Cost,
  MainList.AE_Amt,
  MainList.Status_Type,
  MainList.Date,
  MainList.Remarks,
  StnList.CE_Zone,
  StnList.CWE_Region,
  StnList.GE_Division,
  StnList.LocalIFA,
  StnList.CDA_Office
  MainList.Fund_Alloted
 FROM 

(SELECT
  amwp.workscategorylist.Category_Type,
  amwp.amwplist.Work_ID_by_MoD,
  amwp.amwplist.Nomenclature,
  amwp.amwplist.Cost,
  amwp.worksstatus.AE_Amt,
  amwp.workstatuslist.Status_Type,
  amwp.worksstatus.Date,
  amwp.worksstatus.Remarks,
  amwp.amwplist.Fund_Alloted
FROM
  amwp.amwplist
  LEFT JOIN amwp.workscategorylist ON amwp.workscategorylist.Category_ID = amwp.amwplist.Category_ID
  LEFT JOIN amwp.worksstatus ON amwp.worksstatus.Work_ID_by_MoD = amwp.amwplist.Work_ID_by_MoD
  LEFT JOIN amwp.workstatuslist ON amwp.workstatuslist.Status_ID = amwp.worksstatus.Status_ID) as MainList,
  (SELECT
  amwp.commandhqlist.Command_Name,
  amwp.sectorlist.Sector,
  amwp.stationlist.Station,
  amwp.celist.CE_Zone,
  amwp.cwelist.CWE_Region,
  amwp.gelist.GE_Division,
  amwp.localifalist.LocalIFA,
  amwp.cdalist.CDA_Office
FROM
  amwp.stationlist
  LEFT JOIN amwp.commandhqlist ON amwp.commandhqlist.CommandHQ_ID = amwp.stationlist.Command_ID
  LEFT JOIN amwp.sectorlist ON amwp.sectorlist.Sector_ID = amwp.stationlist.Sector_ID
  LEFT JOIN amwp.celist ON amwp.celist.CE_ID = amwp.stationlist.CE_ID
  LEFT JOIN amwp.cwelist ON amwp.cwelist.CWE_ID = amwp.stationlist.CWE_ID
  LEFT JOIN amwp.gelist ON amwp.gelist.GE_ID = amwp.stationlist.GE_ID
  LEFT JOIN amwp.localifalist ON amwp.localifalist.LocalIFA_ID = amwp.stationlist.LocalIFA_ID
  LEFT JOIN amwp.cdalist ON amwp.cdalist.CDA_ID = amwp.stationlist.CDA_ID) As StnList

 right JOIN StnList ON StnList.Station_ID = MainList.Station_ID 

0 个答案:

没有答案