Mysql 连接具有非齐次属性的四个视图表时出错
我们一直在尝试根据不同的属性将三个视图表连接在一起,但我们收到了一些错误消息,称存在重复项。 我们的代码是这样的:Mysql 连接具有非齐次属性的四个视图表时出错,mysql,Mysql,我们一直在尝试根据不同的属性将三个视图表连接在一起,但我们收到了一些错误消息,称存在重复项。 我们的代码是这样的: create view AllScores as select players.*, topattacker.*, topkeepers.*, topmidfielders.*, topdefenders.* from players inner join topattacker on topattacker.PL_ID = players.PL_ID inner join
create view AllScores as
select players.*, topattacker.*, topkeepers.*, topmidfielders.*, topdefenders.*
from players
inner join topattacker on topattacker.PL_ID = players.PL_ID
inner join topdefenders on topdefenders.PL_ID = players.PL_ID
inner join topkeepers on topkeepers.PL_ID = players.PL_ID
inner join topmidfielders on topmidfielders.PL_ID = players.PL_ID
where players.PL_ID = topattacker.PL_ID and
players.PL_ID = topdefenders.PL_ID and
players.PL_ID = topkeepers.PL_ID and
players.PL_ID = topmidfielders.PL_ID
group by players.PL_ID
问题是有些表具有相同的名称。例如,您有
players.PL\u ID
和topattacker.PL\u ID
由于您使用的是*
,因此您将从所有表中获取所有PL\U ID。在此平台上不能有多个同名列
要解决此问题,请删除所有*
,并列出所需的每列
这在几乎所有情况下都是可取的,但临时查询除外。您只需在select而非表格中明确列名即可。*
create view AllScores as
select disctint p.col1, p.col2, ...
, a,col1, a.col2, ....
, k.col1, k.col2, ....
, f.col1, f.col2 ...
, d.col1, d.col2 , .....
from players p
inner join topattacker a on a.PL_ID = p.PL_ID
inner join topdefenders d on d.PL_ID = a.PL_ID
inner join topkeepers on k k.PL_ID = p.PL_ID
inner join topmidfielders f on f.PL_ID = p.PL_ID
如果不使用聚合函数,则不需要group by,但也不需要distinct。当您已经具有等效的连接条件时,也不需要where。表5不是3,这是什么意思?不要使用
*
列出所有字段名--您将很快看到DUP是什么。尽管删除了.*@GeorgesElias删除。*时,选择列表是什么?