I am using mysql and here is the schema that I have.
First table: Keywords
+------------+-------------+
| keyword_id | keyword_tag |
+------------+-------------+
| 2 | marketing |
| 58 | sales |
| 59 | scraping |
| 1 | seo |
| 3 | testkeyword |
+------------+-------------+
Second table: 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 |
+-----------+-----------------+---------------+---------------------+-----------------+
Third table: 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 |
+-------+-----------+------------+
Fourth table: Emails
+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+
| email_id | email | valid | is_generated | score | number_results | domain_id | user_id |
+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+
| 1 | [email protected] | 1 | 1 | 0.5 | 2 | 1 | 3 |
| 2 | [email protected] | 1 | 1 | 0.3 | 0 | 1 | 1 |
| 3 | [email protected] | 1 | 1 | 0.3 | 0 | 11 | 4 |
| 4 | [email protected] | 1 | 1 | 0.3 | 0 | 11 | 4 |
| 5 | [email protected] | 1 | 1 | 0.3 | 0 | 11 | 1 |
| 6 | [email protected] | 1 | 1 | 0.5 | 3 | 11 | 3 |
+----------+-----------------------+-------+--------------+-------+----------------+-----------+---------+
I want to show the data as following:
Kindly guide me how can I query data from those four tables. Thanks
2
Answers
You may try this. Simple
inner join
withgroup by
clause will give you your desired result. JustGroup_Concat
is use to convert your cities name into one string andcount
is use to count the email records.You may find this fiddle working Link.
Because you can have multiple rows in each table for a given
domain_id
, it’s necessary to perform all aggregation in derived tables, and then join them to theDomains
table.Output:
Demo on dbfiddle