SQL查询:如何从四个不同的表中获取数据

时间:2019-08-28 07:31:11

标签: mysql sql join select

我正在使用mysql,这是我拥有的架构。

第一张桌子:Keywords

+------------+-------------+
| keyword_id | keyword_tag |
+------------+-------------+
|          2 | marketing   |
|         58 | sales       |
|         59 | scraping    |
|          1 | seo         |
|          3 | testkeyword |
+------------+-------------+

第二张表:Domains

+-----------+-----------------+---------------+---------------------+-----------------+
| domain_id | domain_name     | campaign_name | campaign_date       | campaign_note   |
+-----------+-----------------+---------------+---------------------+-----------------+
|         1 | test.org        | campaign 1    | 2019-08-27 17:10:58 | Test            |
|        11 | example.org     | campaign 2    | 2019-08-27 17:36:06 | Campaign Note 2 |
+-----------+-----------------+---------------+---------------------+-----------------+

第三张表:Domain_Keywords

+-------+-----------+------------+
| dk_id | domain_id | keyword_id |
+-------+-----------+------------+
|     1 |         1 |          1 |
|     2 |         1 |          2 |
|     3 |         1 |          3 |
|     4 |        11 |          1 |
|     5 |        11 |          2 |
|     6 |        11 |         58 |
|     7 |        11 |         59 |
+-------+-----------+------------+

第四张表:Emails

+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+
| email_id | email                 | valid | is_generated | score | number_results | domain_id | user_id |
+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+
|        1 | b1@test.org           |     1 |            1 |   0.5 |              2 |        1  |       3 |
|        2 | b2@test.org           |     1 |            1 |   0.3 |              0 |        1  |       1 |
|        3 | a1@example.org        |     1 |            1 |   0.3 |              0 |        11 |       4 |
|        4 | a2@example.org        |     1 |            1 |   0.3 |              0 |        11 |       4 |
|        5 | a3@example.org        |     1 |            1 |   0.3 |              0 |        11 |       1 |
|        6 | a4@example.org        |     1 |            1 |   0.5 |              3 |        11 |       3 |
+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+

我想显示如下数据: enter image description here

请指导我如何从这四个表中查询数据。谢谢

2 个答案:

答案 0 :(得分:2)

由于给定的domain_id在每个表中可以有多行,因此有必要在派生表中执行所有聚合,然后将它们联接到Domains表中。

SELECT d.campaign_name,
       d.campaign_date,
       COALESCE(e.num_emails, 0) AS num_emails,
       COALESCE(e.num_generated_emails, 0) AS num_generated_emails,
       k.keywords
FROM Domains d
JOIN (SELECT dk.domain_id, 
             GROUP_CONCAT(k.keyword_tag) AS keywords
      FROM Domain_Keywords dk
      JOIN Keywords k ON k.keyword_id = dk.keyword_id
      GROUP BY dk.domain_id) k ON k.domain_id = d.domain_id
LEFT JOIN (SELECT domain_id,
                  COUNT(*) AS num_emails,
                  SUM(is_generated) AS num_generated_emails
           FROM Emails
           GROUP BY domain_id) e ON e.domain_id = d.domain_id

输出:

campaign_name   campaign_date       num_emails  num_generated_emails    keywords
campaign 1      2019-08-27 17:10:58 2           2                       seo,marketing,testkeyword
campaign 2      2019-08-27 17:36:06 4           4                       seo,marketing,sales,scraping

Demo on dbfiddle

答案 1 :(得分:1)

您可以尝试一下。简单的inner joingroup by子句将为您提供所需的结果。只是Group_Concat用于将您的城市名称转换为一个字符串,count用于计数电子邮件记录。

您可能会发现Link的这种提琴是有效的。


select Domains.campaign_name, Domains.campaign_date, tab2.countemail as Found, tab2.countemail as generate, tab1.KeyTag 
from Domains inner join 
       (select Domains.domain_id, group_concat(Keywords.keyword_tag) as KeyTag 
         from Domains  
          inner join Domain_Keywords on Domains.domain_id = Domain_Keywords.domain_id 
          inner join Keywords on Domain_Keywords.keyword_id = Keywords.keyword_id  group by  Domains.domain_id ) as tab1 
          on Domains.domain_id = tab1.domain_id
inner join 
         ( select Domains.domain_id, Count(Emails.is_generated) as countemail
          from Domains
           inner join Emails on Domains.domain_id = Emails.domain_id
           group by  Domains.domain_id
          ) as tab2
on Domains.domain_id = tab2.domain_id

​```