繁体   English   中英

SQL:使用外键选择多个表时出现问题

[英]SQL: Problem in selecting multiple tables with foreign key

问题

我也没有完全按照我的意愿..

目标发生

我想为我的数据库创建一个事务视图,其中 output 将是:

客户表
在此处输入图像描述

员工表
员工表

产品表
产品表

交易表
交易表

交易表
[交易表]

我想发生的结构:

点击这里查看

我的 SQL 查询:

SELECT
  customer.CustomerName,
  transactions.TransactionID,
  transactions.Date,
  transactions.Type,
  transactions.Method,
  product.ProductName,
  product.Quantity,
  product.Price,
  employee.EmployeeName
FROM
  customer,
  transactions,
  product,
  employee
WHERE
   customer.CustomerID = transactions.TransactionID

查询 Output:

点击这里查看

您必须使用正确的连接(以及有用的别名)。 transactions通过它们的外键链接到所有其他 3 个表,因此从它开始连接:

SELECT
  c.CustomerName,
  t.TransactionID,
  t.Date,
  t.Type,
  t.Method,
  p.ProductName,
  p.Quantity,
  p.Price,
  e.EmployeeName
FROM transactions t
INNER JOIN customer c ON c.CustomerID = t.CustomerID 
INNER JOIN product p ON p.ProductID = t.ProductID
INNER JOIN employee e ON e.EmployeeID = t.EmployeeID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM