0

I have a database with companies and domains. My goal is to link all social media channels to the domain of every country

SELECT 
  yt.screen_name,
  tw.screen_name,
  gp.id,
  fb.title,
  dom.domain_name,
  dom.type,
  dom.url
FROM
  domain AS dom1
  LEFT JOIN youtube AS yt
    ON yt.company_name = dom1.company_name 
    AND domain AS dom2
  LEFT JOIN twitter AS tw
    ON tw.company_name = dom2.company_name 
    AND domain AS dom3
  LEFT JOIN googleplus AS gp
    ON gp.company_name = dom3.company_name 
    AND domain AS dom4
  LEFT JOIN facebook AS fb
    ON fb.company_name = dom4.company_name 
WHERE 
  yt.screen_name IS NOT NULL
  OR tw.screen_name IS NOT NULL
  OR gp.id IS NOT NULL
  OR fb.title IS NOT NULL
ORDER BY dom.url

my goal is to get a table like this:

yt.screen_name | tw.screen_name | gp.id | fb.title | dom.domain_name | dom.type | dom.url
foo             NULL             NULL    NULL       foo.com           website    http://www.foo.com
NULL            foo              NULL    NULL       foo.com           website    http://www.foo.com 
NULL            NULL             0478174 NULL       foo.com           website    http://www.foo.com

is this possible?

6
  • What is the issue with the query you already have? Commented Jan 22, 2015 at 15:16
  • I get this: #1064 - You have an error in your SQL syntax; Commented Jan 22, 2015 at 15:18
  • company_name is a bad thing to join on as 2 companies could have the same name and the name could change. It's better to have a company_id or similar field. Commented Jan 22, 2015 at 15:19
  • Can you post a sample from the domain table and update your output example to show what it would look like for more than one domain? If I understand the output, it may be a UNION chain you're after with NULL literals. Commented Jan 22, 2015 at 15:22
  • 1
    You cannot do this: AND domain AS dom2 and dom is not defined. Try defining domain once, and compare everything to that instance only, like LEFT JOIN facebook AS fb ON fb.company_name = dom.company_name. Commented Jan 22, 2015 at 15:27

2 Answers 2

2

Try this

SELECT DISTINCT
  yt.screen_name,
  tw.screen_name,
  gp.id,
  fb.title,
  dom1.domain_name,
  dom1.type,
  dom1.url
FROM
  domain AS dom1
  LEFT JOIN youtube AS yt
    ON yt.company_name = dom1.company_name 

  LEFT JOIN twitter AS tw
    ON tw.company_name = dom1.company_name 

  LEFT JOIN googleplus AS gp
    ON gp.company_name = dom1.company_name 

  LEFT JOIN facebook AS fb
    ON fb.company_name = dom1.company_name 
WHERE 
  yt.screen_name IS NOT NULL
  OR tw.screen_name IS NOT NULL
  OR gp.id IS NOT NULL
  OR fb.title IS NOT NULL
ORDER BY dom1.url
Sign up to request clarification or add additional context in comments.

5 Comments

okay the syntax error is gone, but i get strange table with all social media channels are mixed together. imagine my table above -> NULL is gone and there are the names up to a point where the first value switch
You mean NULL is not appearing at all?
Yepp, it will be filled with duplicated stuff
You probably need distinct records. You need to add DISTINCT at the top. Check the edited code.
If that didn't work, you need to rethink the or conditions in 'where' clause
1

What you are attempting to do seems reasonable... and I think the problem here is that you've got some syntax errors in your join conditions. Instead of what you have, try:

SELECT 
  yt.screen_name,
  tw.screen_name,
  gp.id,
  fb.title,
  dom.domain_name,
  dom.type,
  dom.url
FROM
  domain AS dom
  LEFT JOIN youtube AS yt
    ON yt.company_name = dom.company_name 
  LEFT JOIN twitter AS tw
    ON tw.company_name = dom.company_name 
  LEFT JOIN googleplus AS gp
    ON gp.company_name = dom.company_name 
  LEFT JOIN facebook AS fb
    ON fb.company_name = dom.company_name 
WHERE 
  yt.screen_name IS NOT NULL
  OR tw.screen_name IS NOT NULL
  OR gp.id IS NOT NULL
  OR fb.title IS NOT NULL
ORDER BY dom.url

The difference here is that instead of iterating through several versions of dom1, dom2, dom3, we're using the domain table (aliased as dom) and joining all of the company tables to dom based on the company name.

I need to be explicit and point out that

 LEFT JOIN youtube AS yt
    ON yt.company_name = dom1.company_name 
    AND domain AS dom2

is not valid SQL syntax, and also you reference values for dom (eg dom.domain_name, dom.type,etc) without actually having a table that's aliased as dom. Instead you referenced your table as dom1 and then tried to iterate through it. I'm not 100% sure, but I think you're thinking of these dom1, dom2, dom3, etc as references to rows in your domain table, perhaps. If so, that isn't necessary. You have a table (domain, aliased as dom) that you want to join with your company lookup tables (youtube, twitter, googleplus, facebook). You don't need dom1, dom2, etc.

1 Comment

I must also agree with diolemo above about joining on company name... generally that's not a good idea. Likewise, I'm not sure why you need four tables for four different social networks. Seems like you could merge them into one social networks table with an id for the social network to which rows pertain... I think your data model could be improved a bit, if you wanted to work on it. But in the near term, the problem you are describing is basically syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.