我有两个选择查询:

SELECT Name as CategoryName FROM providerscategories where ID in(
Select catId from provider_in_category
where providerUsername = 'a' group by providerUsername
);


另一个是:

SELECT Username, providerName from serviceproviders where Username in(
SELECT providerUsername from usedproviders where Username='Admin')


基本上,它的工作是从usedproviders表中按用户名从usedProviders中获取providerName,然后根据结果获取该UserName和该provider的providerName。
然后,我想检查此提供程序位于哪个类别中并获取此类别名称,并将其添加到该类别的UserName和providerName中。

这是一个sql小提琴链接:SQL Fiddle

所需的结果集是:


  用户名| ProviderName |分类名称

最佳答案

好。让我们从这里开始...以下查询不能解决问题的哪一部分...

SELECT sp.*
     , up.username
     , c.name
     , pc.catid
  FROM usedproviders up
  JOIN serviceproviders sp
    ON sp.username = up.providerusername
  JOIN provider_in_category pc
    ON pc.providerusername = up.providerusername
  JOIN providerscategories c
    ON c.id = pc.catid
 WHERE up.username = 'Admin';
+----------+--------------+----------+--------------+-------+
| Username | providerName | username | name         | catid |
+----------+--------------+----------+--------------+-------+
| a        | providerA    | Admin    | CategoryName |     1 |
| b        | providerB    | Admin    | CategoryName |     1 |
+----------+--------------+----------+--------------+-------+

10-05 19:57