复杂的查询 - 需要帮助!
真的希望有人能帮助我解决这个问题! 我有 6 个表:
产品
prodid
Prodequipment
产品
装备类主要
equipclassor
装备类别
classid
装备功能
装备
classid
设备
equipid
车间设备
workshopid
equipid
产品 – 一些产品的列表
设备 – 一些设备的列表
Prodequipment – 列出生产产品所需的设备。您可以使用equipclassmain中列出的设备或将其替换为equipclassor中的设备。表Products 与表Prodequipment 具有一对多关系,即您将使用许多不同的工具(设备)来生产一种产品,但您可以选择使用equipclassmain/equipclassor 对中的任何一个。例如,要给照片加框,您可以使用木框或塑料框(一对)和盖玻璃或盖塑料(第二对)。您可以根据需要组合它,但两对都应该使用:木框架加盖玻璃或塑料框架加盖玻璃或木框架加塑料盖或塑料框架加塑料盖。
设备功能和设备类别 – 由于一件设备可以以不同的方式使用,因此它不直接链接到表 Prodequipment。我创建了 Equipclasses 表,其中列出了每种可能设备的所有单一用途,并创建了 Equipfunctions 表,其中列出了每种设备的单一用途。
车间设备 - 列出他们正在使用的车间和设备。
现在我需要一个可以由两个不同的给定车间制造的产品列表(假设workshopid = 1和workshopid = 4),即这两个车间都拥有生产这些产品所需的所有设备。请记住,这些车间不必使用与我上面描述的相同的设备来执行此操作。
我正在尝试执行此查询:
SELECT prodid FROM Products JOIN (
SELECT workshopid, prodlist, equipclassmain, equipclassor,
if( LOCATE( equipclassmain, prodlist ) >0
AND LOCATE( equipclassor, prodlist ) >0, 1, 0 ) AS pairstatus FROM Prodequipment JOIN
(
SELECT classid FROM Equipclasses JOIN (
SELECT classid FROM Equipfunctions JOIN (
SELECT workshopid, GROUP_CONCAT( equipid ) AS prodlist FROM Workshopequipment
GROUP BY workshopid
)
equipfunclist GROUP BY equipid
) equipclasslist GROUP BY classid
) WorkshopequipmentList HAVING pairstatus = 1 AND workshopid in (1, 4)
) prodbyworkshops ON classid = equipclassmain OR classid = equipclassor
但我收到“字段列表中的列 classid 不明确”。知道这里出了什么问题吗?
谢谢你!
really hope someone can help me on this one!
I have 6 tables:
Products
prodid
Prodequipment
prodid
equipclassmain
equipclassor
Equipclasses
classid
Equipfunctions
equipid
classid
Equipment
equipid
Workshopequipment
workshopid
equipid
Products – a list of some products
Equipment – a list of some equipment
Prodequipment – lists what equipment is needed to do a product. You can use equipment listed in equipclassmain or replace it by equipment in equipclassor. Table Products has one to many relation to table Prodequipment, i.e. you will use many different tools (equipment) to produce one product, but you can choose to use anyone in the pair equipclassmain/equipclassor. For instance to frame a photo you can use a wooden frame or plastic frame (one pair) and a cover glass or cover plastic (second pair). You can combine it as you wish, but both pairs should be used: wooden frame with cover glass or plastic frame with cover glass or wooden frame with plastic cover or plastic frame with plastic cover.
Equipfunctions and Equipclasses – Because one piece of equipment can be used in different ways it is not linked directly to table Prodequipment. I have created table Equipclasses where all single use of every possible equipment is listed and table Equipfunctions where I list those single uses for every equipment.
Workshopequipment – lists workshops and equipment they are using.
Now I need a list of products which can be manufactured by two different given workshops (let's say workshopid = 1 and workshopid = 4), i.e. both those workshops have all equipment needed to produce those products. Bear in mind that those workhops don't have to use the same equipment to do so as I described above.
I'm trying with this query:
SELECT prodid FROM Products JOIN (
SELECT workshopid, prodlist, equipclassmain, equipclassor,
if( LOCATE( equipclassmain, prodlist ) >0
AND LOCATE( equipclassor, prodlist ) >0, 1, 0 ) AS pairstatus FROM Prodequipment JOIN
(
SELECT classid FROM Equipclasses JOIN (
SELECT classid FROM Equipfunctions JOIN (
SELECT workshopid, GROUP_CONCAT( equipid ) AS prodlist FROM Workshopequipment
GROUP BY workshopid
)
equipfunclist GROUP BY equipid
) equipclasslist GROUP BY classid
) WorkshopequipmentList HAVING pairstatus = 1 AND workshopid in (1, 4)
) prodbyworkshops ON classid = equipclassmain OR classid = equipclassor
But I get an "Column classid in field list is ambiguous". Any idea what's wrong here?
THANK YOU!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询引用了多个具有
classid
列的表。因此,当您在
HAVING
子句中引用classid
时,它不知道要获取哪个表的classid
。您需要编写
tablename.classid
,其中tablename
是包含classid
列的表的名称。 (可能是equipclasslist
;我没有查看查询)Your query references multiple tables that have a
classid
column.Therefore, when you reference
classid
in theHAVING
clause, it doesn't know which table to get theclassid
for.You need to write
tablename.classid
wheretablename
is the name of the table containing theclassid
column. (Probablyequipclasslist
; I didn't look at the query)它来自查询的部分:
EquipClasses 和内部查询都有一个 classId。您需要指定您要选择的内容。
It's from the part of the query with:
Both EquipClasses and the inner query have a classId. You need to specify which you're selecting.