Nopcommerce 导出用户角色
Nopcommerce export user roles
我正在尝试通过 nopcommerce 数据库获取每个用户的所有用户角色列表。我只需要 Customer.Id、Customer.Username、Customer.Email 和 Customer.Role(有些有多个角色)
这都是顶起来的。
SELECT Customer.Id, Customer.Username, Customer.Email, CustomerRole.Name
FROM ((Customer
INNER JOIN CustomerRole ON CustomerRole.Id = Customer.Id)
INNER JOIN Customer_CustomerRole_Mapping ON Customer_CustomerRole_Mapping.CustomerRole_Id = CustomerRole.Id)
非常感谢任何帮助!
我认为您需要一个 LEFT OTUER JOIN,因为在您的数据库中可以是没有角色的用户;在这种情况下,您可以使用:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, CR.Id, CR.Name,
CCRM.CustomerRole_Id ,
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
此外,每个用户只能有一行,一列包含他的角色,如下所示:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email,
Roles = STUFF((
SELECT ',' + CAST(CR1.Name as varchar(4000))
FROM CustomerRole CR1
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM1
ON CCRM1.CustomerRole_Id = CR1.Id
LEFT OUTER JOIN CUSTOMER CUST1
ON CUST1.ID = CCRM1.Customer_Id
WHERE CUST1.Id = CUST.Id
FOR XML PATH('')
), 1, 1, '')
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
根据您的使用情况,如果您有很多客户,上次查询可能会有点慢。
我正在尝试通过 nopcommerce 数据库获取每个用户的所有用户角色列表。我只需要 Customer.Id、Customer.Username、Customer.Email 和 Customer.Role(有些有多个角色)
这都是顶起来的。
SELECT Customer.Id, Customer.Username, Customer.Email, CustomerRole.Name
FROM ((Customer
INNER JOIN CustomerRole ON CustomerRole.Id = Customer.Id)
INNER JOIN Customer_CustomerRole_Mapping ON Customer_CustomerRole_Mapping.CustomerRole_Id = CustomerRole.Id)
非常感谢任何帮助!
我认为您需要一个 LEFT OTUER JOIN,因为在您的数据库中可以是没有角色的用户;在这种情况下,您可以使用:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, CR.Id, CR.Name,
CCRM.CustomerRole_Id ,
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
此外,每个用户只能有一行,一列包含他的角色,如下所示:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email,
Roles = STUFF((
SELECT ',' + CAST(CR1.Name as varchar(4000))
FROM CustomerRole CR1
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM1
ON CCRM1.CustomerRole_Id = CR1.Id
LEFT OUTER JOIN CUSTOMER CUST1
ON CUST1.ID = CCRM1.Customer_Id
WHERE CUST1.Id = CUST.Id
FOR XML PATH('')
), 1, 1, '')
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
根据您的使用情况,如果您有很多客户,上次查询可能会有点慢。