【问题标题】:Create Table using inner join in sql在sql中使用内连接创建表
【发布时间】:2015-01-05 07:38:00
【问题描述】:

我应该使用内部连接和另一个表在 sql 中创建一个表。我提出了这样一个解决方案,但 MySQL Workbench 一直告诉我我的 SQL 语法有错误。我没有任何想法。如果有人能帮助我,我将不胜感激。

 CREATE TABLE BigTable AS( 
 SELECT
    ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued
 FROM `northwind`.`products`
 INNER JOIN(
     SELECT 
        CustomerID, EmployeeID, OrderDate, RequiredDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry, ProductID, UnitPrice, Quantity, Discount 
     FROM `northwind`.`orders` 
     INNER JOIN `northwind`.`order_details`
     ON orders.OrderID=order_details.OrderID) AS products
 INNER JOIN(
     SELECT
        LastName, FirstName ,Title ,TitleOfCourtesy ,BirthDate ,HireDate ,Address ,City , Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo, CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,
     FROM `northwind`.`employees`
     INNER JOIN `northwind`.`customers`) AS People));

【问题讨论】:

  • 不要在AS后面加上括号。

标签: mysql join mysql-workbench


【解决方案1】:

去掉外面的括号。应该是:

CREATE TABLE BigTable AS
SELECT
    ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued
FROM `northwind`.`products`
INNER JOIN(
    SELECT 
        CustomerID, EmployeeID, OrderDate, RequiredDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry, ProductID, UnitPrice, Quantity, Discount 
    FROM `northwind`.`orders` 
    INNER JOIN `northwind`.`order_details`
    ON orders.OrderID=order_details.OrderID) AS products
INNER JOIN(
    SELECT
        LastName, FirstName ,Title ,TitleOfCourtesy ,BirthDate ,HireDate ,Address ,City , Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo, CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax
    FROM `northwind`.`employees`
    INNER JOIN `northwind`.`customers`) AS People;

你还有不匹配的括号,因为你在末尾有两个 )) 与开头的 ( 匹配,并且在 Fax 之后有一个额外的 ,

这些将修复语法错误。但看起来您也有逻辑错误,因为您的大多数 INNER JOIN 子句都缺少关联表的 ON 条件。而且您选择了许多您从未使用过的列。

【讨论】:

  • 非常感谢。我试图这样做,因为 CREATE TABLE BigTable AS( SELECT CustomerID, EmployeeID, OrderDate, RequiredDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry, ProductID, UnitPrice, Quantity, Discount FROM @987654328 @.orders INNER JOIN northwind.order_details ON orders.OrderID=order_details.OrderID );已经奏效了:)我会尽量听从你的提示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多