#1066 - 错误不是唯一的表别名

Posted

技术标签:

【中文标题】#1066 - 错误不是唯一的表别名【英文标题】:#1066 - error not unique table alias 【发布时间】:2012-04-06 10:00:10 【问题描述】:

我正在学习 mysql,但我的一张表遇到了问题,它产生了 。我尝试了许多不同的查询,但结果都相同。有人可以让我摆脱痛苦并帮助指出我做错了什么! - 谢谢

表格

looks
---------

look_id(Pk)
user_id
title
description
date_modified

Add_Look_Item
-----------------------

look_id (fk from table 1)
item_id (fk from table 3)

Item
--------------

item_id (pk)
name
details

Add_Images
-------------------
image_id (pk)
item_id (fk from table 3)
image_name

我尝试过的查询 - add_look_item 是一个复合键

SELECT DISTINCT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id,     add_images.image_name FROM looks JOIN add_look_item ON add_look_item.look_id = looks.look_id
JOIN add_look_item ON add_look_item.item_id = item.item_id WHERE look.user_id = 94

#1066 - error not unique table alias 'add_look_item

SELECT DISTINCT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id,     add_images.image_name FROM looks JOIN 
add_look_item ON add_look_item.looks_id = look.look_id
JOIN add_look_item ON add_look_item.item_id = item.item_id
JOIN add_images ON add_images.item_id = item.item_id WHERE add_look_item.item_id =    item.item_id AND look.user_id = 94

#1066 - error not unique table alias 'add_look_item

SELECT DISTINCT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id,   add_images.image_name FROM looks INNER JOIN add_look_item ON add_look_item.look_id =    look_look_id
INNER JOIN add_images ON add_images.item_id = item.item_id
INNER JOIN add_look_item ON add_look_item.item_id = item.item_id
WHERE look.user_id = 94

#1066 - error not unique table alias 'add_look_item

SELECT DISTINCT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id,    add_images.image_name FROM looks 
INNER JOIN add_look_item ON add_look_item.looks_id = look.look_id
INNER JOIN add_images ON add_images.item_id = item.item_id
INNER JOIN add_look_item ON add_look_item.item_id = item.item_id 
WHERE add_look_item.item_id = item.item_id AND look.user_id = 94


#1066 - error not unique table alias 'add_look_item

SELECT DISTINCT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id,    add_images.image_name FROM looks 
INNER JOIN add_look_item ON add_look_item.looks_id = look.look_id
INNER JOIN add_images ON add_images.item_id = item.item_id
WHERE add_look_item.item_id = item.item_id AND look.user_id = 94

#1054 - Unknown column 'item.item_id' in 'where clause'

谢谢

【问题讨论】:

我需要这些值looks.look_id、looks.title、looks.date_modified、add_look_item.item_id、add_images.image_name,谢谢 如果你只是想加入同一个表,为什么要多次加入? 我理解的方式是我正在访问 4 个表中的值,所以我需要加入这些表?还是有更简单更好的方法? 【参考方案1】:

试试这个 -

SELECT looks.look_id, looks.title, looks.date_modified, add_look_item.item_id, add_images.image_name
FROM looks
JOIN add_look_item
    ON looks.look_id = add_look_item.look_id
JOIN add_images
    ON add_look_item.item_id = add_images.item_id
WHERE look.user_id = 94

【讨论】:

感谢 nnichols 但返回 #1054 - 'on Clause' 中的未知列 'ali1.item_id' 一定是因为 ali1 尚未设置?【参考方案2】:

为 add_look_item 创建一个别名,因为您要多次加入它。

例如:

SELECT tbl.a, a.y, b.y 
FROM tbl 
JOIN tbl2 as a ON tbl.x = a.x 
JOIN tbl2 as b ON tbl.x = b.x

否则查询引擎将不知道该怎么做:)

【讨论】:

以上是关于#1066 - 错误不是唯一的表别名的主要内容,如果未能解决你的问题,请参考以下文章

发生数据库错误错误号:1066 不是唯一的表/别名:[重复]

为啥错误 #1066 - 不是唯一的表/别名:'cat_rapoarte'

错误代码:1066。不是唯一的表/别名:'ordertbl' - 无法执行内部连接 ​​MYSQL

#1066 - 错误不是唯一的表别名

Laravel - 语法错误或访问冲突:1066 不是唯一的表/别名

帮助 MySQL 查询语法:错误 #1066 - 不是唯一的表/别名