gpt4 book ai didi

mysql - 我是否错误地进行了嵌套连接?

转载 作者:行者123 更新时间:2023-11-29 05:07:57 26 4
gpt4 key购买 nike

伙计们!我在这个问题上挠头。就在我在上次加入中添加 UserRole 部分时,我开始看到此错误。我究竟做错了什么? MySQL 中嵌套连接的数量是否有限制?

错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON Opportunity.AccountId = AccountD.Id' at line 31

表结构

Opportunity
-----------
Id, Name, AccountId, OwnerId

Account
-------
Id,Name,OwnerId

User
----
Id,Name,UserRoleId

UserRole
--------
Id,Name

RecordType
----------
Id,Name

SQL

SELECT 
Opportunity.Id AS 'Opportunity_ID',
Opportunity.Opportunity_Number__c AS 'Opportunity_Number__c',
UserA.Name AS 'Opportunity_Owner',
Opportunity.Name AS 'Opportunity_Name',
Opportunity.Probability AS 'Opportunity_Probability',
Opportunity.StageName AS 'Opportunity_Stage',
Opportunity.Amount AS 'Opportunity_Amount',
Opportunity.CloseDate AS 'Opportunity_CloseDate',
Opportunity.Roll_Out_End_Formula__c AS 'Opportunity_Rollout_End_Date',
RecordTypeA.Name AS 'Record_Type',
Opportunity.Division__c AS 'Division',
Pricebook2.Name AS 'Price_Book',
Opportunity.Won_Date__c AS 'Opportunity_Won_Date',
Opportunity.CreatedDate AS 'Opportunity_Created_Date',
AccountA.Id AS 'Account_ID',
AccountA.Name AS 'Account_Name',
AccountA.Type AS 'Account_Type',
RecordTypeB.Name AS 'Account_Record_Type',
AccountA.Key_Account__c AS 'Key_Account',
UserB.Name AS 'Account_Owner',
UserB.Sales_Team__c AS 'Account_Owner_Sales_Team',
UserRoleA.Name AS 'Account_Owner_User_Role'
FROM Opportunity
LEFT JOIN User UserA ON Opportunity.OwnerId = UserA.Id
LEFT JOIN RecordType RecordTypeA ON Opportunity.RecordTypeId = RecordTypeA.Id
LEFT JOIN Pricebook2 ON Opportunity.Pricebook2Id = Pricebook2.Id
LEFT JOIN Account AccountA ON Opportunity.AccountId = AccountA.Id
LEFT JOIN Account AccountB JOIN RecordType RecordTypeB ON AccountB.RecordTypeId = RecordTypeB.Id ON Opportunity.AccountId = AccountB.Id
LEFT JOIN Account AccountC JOIN User UserB ON AccountC.OwnerId = UserB.Id ON Opportunity.AccountId = AccountC.Id
LEFT JOIN Account AccountD JOIN User UserC JOIN UserRole UserRoleA ON UserC.UserRoleId = UserRoleA.Id ON AccountD.OwnerId = UserC.Id ON Opportunity.AccountId = AccountD.Id
LIMIT 5\G

最佳答案

如果您想对“帐户”实例进行嵌套连接,则必须使用括号。

LEFT JOIN Account AccountA ON Opportunity.AccountId = AccountA.Id
LEFT JOIN (Account AccountB JOIN RecordType RecordTypeB ON AccountB.RecordTypeId = RecordTypeB.Id)
ON Opportunity.AccountId = AccountB.Id
LEFT JOIN (Account AccountC JOIN User UserB ON AccountC.OwnerId = UserB.Id)
ON Opportunity.AccountId = AccountC.Id
LEFT JOIN (Account AccountD JOIN User UserC ON AccountD.OwnerId = UserC.Id JOIN UserRole UserRoleA ON UserC.UserRoleId = UserRoleA.Id)
ON Opportunity.AccountId = AccountD.Id

编辑:我修复了最后一行(带有 AccountD 的那一行)。

关于mysql - 我是否错误地进行了嵌套连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44831012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com