简体   繁体   English

MySQL通过关系表联接

[英]MySQL join through relational table

Hi I can't seem to find the right way to write this query. 嗨,我似乎找不到正确的方法来编写此查询。 I have two entities websites and clients, and a table that relates them through their id fields. 我有两个实体网站和客户,以及一个通过其ID字段将它们关联的表格。

This is a many to many relationship. 这是多对多的关系。 ie a website can have multiple clients and a client can have multiple websites. 即一个网站可以有多个客户,一个客户可以有多个网站。

I am trying to write a query that returns all the websites with the clients that belong to them. 我试图编写一个查询,以返回所有网站以及属于它们的客户端。 I want to return all the websites even if they have no clients associated with them. 我想退回所有网站,即使它们没有与之关联的客户也是如此。 Here is the query that I am working with at the moment: 这是我目前正在使用的查询:

the three tables are ost_sites = websites, ost_site_auth = relational table, ost_clients = clients 这三个表是ost_sites =网站,ost_site_auth =关系表,ost_clients =客户

SELECT 
    ost_sites.site_id, 
    ost_sites.name,
    ost_sites.site_url,
    ost_site_auth.site_id,
    ost_site_auth.client_id 
    ost_clients.client_id,
    CONCAT_WS(" ", ost_clients.lastname, ost_clients.firstname) as name,
FROM ost_sites
LEFT JOIN (ost_site_auth, ost_clients) 
    ON (ost_sites.site_id=ost_site_auth.site_id 
        AND ost_site_auth.client_id=ost_clients.client_id)
GROUP BY ost_sites.name

I get a result set but it doesn't return all the sites, and all of the rows don't have clients associated with them. 我得到一个结果集,但它不会返回所有站点,并且所有行都没有与之关联的客户端。

Thanks so much for any help! 非常感谢您的帮助!

Edit: 编辑:

Here are the columns for the tables: 这是表格的列:

ost_site ost_site

site_id |    name    | site_url
1          facebook    facebook.com
2          twitter     twitter.com
3          tubmblr     tumblr.com
4          google      google.com

ost_site_auth ost_site_auth

(notice no site_id = 3 in auth list) (注意,身份验证列表中没有site_id = 3)

id |   site_id    | client_id
1        1             1
2        1             2
3        2             1 
4        2             2
5        4             1
6        4             4

ost_client ost_client

client_id  |  firstname  |  lastname
1              wilma         flintstone
2              bam           bam
3              fred          flintstone
4              barney        rubble

expected output: 预期输出:

site_id |    name    |    site_url    |      client_name     |
1          facebook    facebook.com        wilma flintstone
1          facebook    facebook.com        bam bam
2          twitter     twitter.com         wilma flintstone
2          twitter     twitter.com         bam bam
4          google      google.com          wilma flintstone
4          google      google.com          barney rubble
3          tumblr      tumlr.com           NULL

Your join looks a bit off... try this 您的加入似乎有点...尝试一下

SELECT 
    ost_sites.site_id, 
    ost_sites.name,
    ost_sites.site_url,
    ost_site_auth.site_id,
    ost_site_auth.client_id 
    ost_clients.client_id,
    CONCAT_WS(" ", ost_clients.lastname, ost_clients.firstname) as name

FROM ost_sites

LEFT OUTER JOIN ost_site_auth 
    ON ost_sites.site_id=ost_site_auth.site_id 

LEFT OUTER JOIN ost_clients
    ON ost_site_auth.client_id=ost_clients.client_id

ORDER BY ost_sites.name

Let me try to explain this a little for you... 让我尝试为您解释一下...

  • We start with the ost_sites table and we want all the results from that regardless of if anything matches in the other tables. 我们从ost_sites表开始,我们希望从中获得所有结果,而不管其他表是否匹配。
  • Then, we do a left outer join to the table ost_site_auth . 然后,我们对表ost_site_auth进行left outer join ost_site_auth That means that if something from ost_site_auth does not match something in ost_sites , it will not be returned. 这意味着,如果从一些ost_site_auth不匹配的东西ost_sites ,也不会退还。 However, something in ost_sites that doesn't match something in ost_site_auth will be returned because of the left outer part. 但是,由于left outer将返回ost_site_authost_sites中的内容不匹配的内容。
  • Next, we repeat the left outer join for the ost_clients . 接下来,我们为ost_clients重复left outer join ost_clients


Not sure what you want... Let's pretend we have this data represented in the tables: 不确定您想要什么...让我们假设这些数据在表中表示:

  • Site #1 has no clients 网站#1没有客户端
  • Site #2 has one client: A 站点2具有一个客户端:A
  • Site #3 has two clients: B, C 站点3具有两个客户端:B,C
  • Site #4 has three clients: D, E, F 站点#4具有三个客户端:D,E,F
  • Site #5 has no clients 网站#5没有客户
  • Clients G and H have no associated site 客户G和H没有关联的站点

Query One 查询一

      SELECT 
          ost_sites.site_id as SITE,   
          ost_clients.client_id as CLIENT

      FROM ost_sites

      LEFT OUTER JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      LEFT OUTER JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      ORDER BY ost_sites.site_id, ost_clients.client_id

That would return (basically) 那会回来(基本上)

SITE     CLIENT 
1        NULL
2        A
3        B
3        C
4        D
4        E
4        F
5        NULL

Query Two 查询二

      SELECT 
          ost_sites.site_id as SITE,   
          ost_clients.client_id as CLIENT

      FROM ost_sites

      JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      ORDER BY ost_sites.site_id, ost_clients.client_id

That would return (basically) 那会回来(基本上)

SITE     CLIENT  
2        A
3        B
3        C
4        D
4        E
4        F 

Query three 查询三

      SELECT 
          ost_sites.site_id as SITE,   
          ost_clients.client_id as CLIENT

      FROM ost_sites

      FULL OUTER JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      FULL OUTER JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      ORDER BY ost_sites.site_id, ost_clients.client_id

That would return (basically) 那会回来(基本上)

SITE     CLIENT  
1        NULL
2        A
3        B
3        C
4        D
4        E
4        F
5        NULL
NULL     G
NULL     H 

Query four 查询四

      SELECT DISTINCT ost_sites.site_id as SITE 

      FROM ost_sites

      LEFT OUTER JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      LEFT OUTER JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      ORDER BY ost_sites.site_id 
      ORDER BY ost_sites.site_id 

That would return (basically) 那会回来(基本上)

SITE         
2
3
4 

Query five 查询五

      SELECT 
          ost_sites.site_id as SITE,
          count(ost_clients.client_id) as CLIENT_COUNT

      FROM ost_sites

      JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      GROUP BY ost_sites.site_id 
      ORDER BY ost_sites.site_id 

That would return (basically) 那会回来(基本上)

SITE        CLIENT_COUNT  
2           1
3           2
4           3

Query five 查询五

      SELECT 
          ost_sites.site_id as SITE,
          count(ost_clients.client_id) as CLIENT_COUNT

      FROM ost_sites

      LEFT OUTER JOIN ost_site_auth 
          ON ost_sites.site_id=ost_site_auth.site_id 

      LEFT OUTER JOIN ost_clients
          ON ost_site_auth.client_id=ost_clients.client_id

      GROUP BY ost_sites.site_id 
      ORDER BY ost_sites.site_id 

That would return (basically) 那会回来(基本上)

SITE        CLIENT_COUNT  
1           0
2           1
3           2
4           3
5           0

Check out 查看

Full Outer Join in MySQL 在MySQL中完全外加入

I think all you really need to do is do the same query as a right outer join as well and union them. 我认为您真正需要做的就是也进行与正确的外部联接相同的查询并将其合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM