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?
#1064 - You have an error in your SQL syntax;company_nameis a bad thing to join on as 2 companies could have the same name and the name could change. It's better to have acompany_idor similar field.domaintable 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 aUNIONchain you're after withNULLliterals.AND domain AS dom2anddomis not defined. Try defining domain once, and compare everything to that instance only, likeLEFT JOIN facebook AS fb ON fb.company_name = dom.company_name.