mysql - 在 SQL 中创建包含三个表的 View

标签 mysql sql view

我想创建一个 View ,其中包含我拥有的 3 个表中的所有记录。但是我收到错误,我不知道如何解决它。

错误是 Customer.customerID 重复。

Create view overview as
Select
customer.customerID,
customer.username, 
customer.name, 
customer.surname, 
customer.city, 
customer.street, 
customer.zipcode, 
customer.birthdate, 
customer.`payment method`,
customer.screenname,
order.OrderID,
order.customerID,
order.date,
order.GameID,
games.GameID,
games.game,
games.price,
games.released,
games.genre,
games.grid
FROM customer
INNER JOIN order
ON customer.customerID = order.customerID
INNER JOIN games
ON games.GameID = order.GameID

表格及其行:

订单 订单ID、客户ID、日期、游戏ID

游戏 GameID、游戏、价格、发布、类型、网格

客户 customerID、用户名、姓名、城市、街道、邮政编码、出生日期、付款方式、屏幕名称

请帮忙。

最佳答案

CustomerID 列被选择两次且名称相同,您必须使用别名 AS ... 为其中一个列指定不同的名称,如下所示:

Create view overview as
Select
customer.customerID,
customer.username, 
customer.name, 
customer.surname, 
customer.city, 
customer.street, 
customer.zipcode, 
customer.birthdate, 
customer.`payment method`,
customer.screenname,
order.OrderID,
order.customerID AS OrderCustomerId, -- this
order.date,
order.GameID,
games.GameID gamesGameID,           -- This too
games.game,
games.price,
games.released,
games.genre,
games.grid
FROM customer
INNER JOIN order
ON customer.customerID = order.customerID
INNER JOIN games  ON games.GameID = order.GameID

或者将其从选择中删除,因为它与 Customers.CustomerId 的值相同。

关于mysql - 在 SQL 中创建包含三个表的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27525602/

相关文章:

android - 触摸时更改 ListView 行内的背景颜色和 View 颜色

mysql - 如何连接payara和MySQL?

mysql - Magento2 With Redis,错误:MySQL适配器:缺少必需的配置选项 'host'(有时)

mysql - 使用 MYSQL 处理不明确的列名

ruby-on-rails - Rails 3.2 - haml 与 erb。哈姆更快吗? (2012 年 2 月)

java - 在Android中绘制动态自定义 View 棋盘

php - 尝试在 Laravel 中回滚迁移时出现回退错误

mysql - 如何将所有 WordPress 内容从一台主机迁移到另一台主机?

mysql - 从搜索记录中获取搜索次数最多的术语,忽略任何用户在给定时间范围(小时)内对搜索术语的任何重复条目

sql - 用同一列中的先前值填充空值