错误1054-“子句”中的未知列“ Store.Orders_ID”

时间:2018-11-08 12:22:24

标签: mysql

我正在为一个游戏租赁店建立一个数据库。除了一件事,一切都运转良好。当我尝试将Orders(table)联接到Staff(table)上时,它在“ on子句”中显示错误“未知列“ Staff.Orders_ID””。现在我工作了,我加入了工作人员(表)到订单(表)上。除薪水查询外,一切工作正常,薪水查询从Orders(table)获取薪水,因此薪水总和将随每次销售而增加。不确定我的表是否与外键或其他东西正确连接。

存储表

USE `mydb` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Store` (
  `Store_ID` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,  

      `Name` VARCHAR(45) NOT NULL,
      `Country` VARCHAR(45) NOT NULL,
      `City` VARCHAR(45) NOT NULL,
      `Address` VARCHAR(255) NOT NULL,
      `Phone` VARCHAR(20) NOT NULL,  
      `Email` VARCHAR(255) NULL DEFAULT NULL,
      `Store_Rent` DECIMAL(9.2) NOT NULL DEFAULT 500,
      `Store_Utilities` DECIMAL(9.2) NOT NULL DEFAULT 200,

  `Last_Update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`Store_ID`))     
 ENGINE = InnoDB AUTO_INCREMENT=1;

库存表

USE `mydb` ;                                        
CREATE TABLE IF NOT EXISTS `mydb`.`Inventory` (
  `Inventory_ID` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,

      `Title` VARCHAR(45) NOT NULL DEFAULT "UNKNOWN TITLE",
      `Rental_Rate_Per_Day` DECIMAL(4,2) NOT NULL DEFAULT 5.99,
      `Supplier_Cost` DECIMAL(4,2) NOT NULL DEFAULT 29.99,
      `Units_in_Stock` INT(4) NOT NULL DEFAULT 0, 
      `Store_ID` INT(5) UNSIGNED NOT NULL,

   `Last_Update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
   INDEX `fk_Inventory_Store_idx` (`Store_ID` ASC) VISIBLE,

   CONSTRAINT `fk_Inventory_Store`
        FOREIGN KEY (`Store_ID`)
        REFERENCES `mydb`.`Store` (`Store_ID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE,

PRIMARY KEY (`Inventory_ID`))
ENGINE = InnoDB AUTO_INCREMENT=1;

职员表

USE `mydb` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Staff` (
`Staff_ID` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, 

      `Date_Joined` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `Name` VARCHAR(45) NOT NULL,
      `Country` VARCHAR(45) NOT NULL,
      `City` VARCHAR(45) NOT NULL,
      `Address` VARCHAR(255) NOT NULL,
      `Phone` VARCHAR(20) NOT NULL,  
      `Email` VARCHAR(255) NULL DEFAULT NULL,
      `Salary` DECIMAL(9.2) NOT NULL DEFAULT 2000,
      `Store_ID` INT(5) UNSIGNED NOT NULL,

  INDEX `fk_Staff_Store_idx` (`Store_ID` ASC) VISIBLE,
  CONSTRAINT `fk_Staff_Store`
      FOREIGN KEY (`Store_ID`)
      REFERENCES `mydb`.`Store` (`Store_ID`)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
  `Last_Update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Staff_ID`))
ENGINE = InnoDB AUTO_INCREMENT=1;

订单表

USE `mydb` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Orders` (
`Orders_ID` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,  

      `Order_Date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `Store_ID` INT(5) UNSIGNED NOT NULL,
      `Staff_ID` INT(5) UNSIGNED NOT NULL,
      `Inventory_ID` INT(5) UNSIGNED NOT NULL,    
      `Customer_ID` INT(5) UNSIGNED NOT NULL,
      `Return_Date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,  
      -- `Rental_Number_of_Days` INT(5) NOT NULL,
      `Quantity` INT(3) NOT NULL,


 `Last_Update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`Orders_ID`),

 INDEX `fk_Orders_Store_idx` (`Store_ID` ASC) VISIBLE,
 INDEX `fk_Orders_Staff_idx` (`Staff_ID` ASC) VISIBLE,
 INDEX `fk_Orders_Inventory_idx` (`Inventory_ID` ASC) VISIBLE,
 INDEX `fk_Orders_Customer_idx` (`Customer_ID` ASC) VISIBLE,

 CONSTRAINT `fk_Orders_Store`
        FOREIGN KEY (`Store_ID`)
        REFERENCES `mydb`.`Store` (`Store_ID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
 CONSTRAINT `fk_Orders_Customer`
        FOREIGN KEY (`Customer_ID`)
        REFERENCES `mydb`.`Customer` (`Customer_ID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
 CONSTRAINT `fk_Orders_Staff`
        FOREIGN KEY (`Staff_ID`)
        REFERENCES `mydb`.`Staff` (`Staff_ID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
 CONSTRAINT `fk_Orders_Inventory`
        FOREIGN KEY (`Inventory_ID`)
        REFERENCES `mydb`.`Inventory` (`Inventory_ID`)
        ON DELETE CASCADE
        ON UPDATE CASCADE)
 ENGINE = InnoDB AUTO_INCREMENT=1;

查询

USE `mydb` ;
CREATE VIEW Expenses_All AS 

SELECT  Store.Name AS "Store Name", 
        CAST(SUM(Inventory.Supplier_Cost * Inventory.Units_in_Stock) AS 
        DECIMAL(5.2)) AS "Value of Games in Stock",
        CAST(SUM(Inventory.Rental_Rate_Per_Day * Orders.Quantity * 
        (datediff(Return_Date, Order_Date))) AS DECIMAL(5.2)) AS "Sales in 
        Euro",
        CAST(SUM(Salary) AS DECIMAL(5.2)) AS "Monthly Salaries",
        Store_Rent AS "Monthly Rent Pay",
        Store_Utilities AS "Monthly Utilities Pay",
        CAST((SUM(Inventory.Rental_Rate_Per_Day * Orders.Quantity * 
        (datediff(Return_Date, Order_Date)))) - Store_Rent - Store_Utilities 
        - (SUM(Salary)) AS DECIMAL(5.2)) AS "Monthly Profit"

    /*  

    FROM (((Store  -- <-- Unknown column "Store.Orders_ID" in "on clause"
        LEFT JOIN Staff ON Store.Staff_ID = Staff.Staff_ID)
        LEFT JOIN Inventory ON Store.Inventory_ID = Inventory.Inventory_ID)
        LEFT JOIN Orders ON Store.Orders_ID = Orders.Orders_ID)

    GROUP BY Store.Store_ID;   

    */

    FROM (((Orders -- <-- This gives me the wrong salary
        LEFT JOIN Staff ON Orders.Staff_ID = Staff.Staff_ID)
        LEFT JOIN Inventory ON Orders.Inventory_ID =Inventory.Inventory_ID)
        LEFT JOIN Store ON Orders.Store_ID = Store.Store_ID)

    GROUP BY Store.Store_ID;

Photo with the output

0 个答案:

没有答案