mysql查询有助于通过连接多个表来获取数据

时间:2012-09-23 12:06:53

标签: mysql

我有以下表格:

基: id | domain

扩展: id | domain | country_code

国家: id | country_code | country_name

banned_domains: id | domain

我将在banned_domains中拥有数千个(超过500K)的域名。现在我需要获取banned_domains列表中不存在的数据“domain,country_code和country_name”。我不是很擅长MySQL JOINS,任何人都可以指导我进行正确的查询。

2 个答案:

答案 0 :(得分:1)

SELECT b.domain, ex.country_code, c.country_name FROM base b 
INNER JOIN extended ex ON b.domain=ex.domain
INNER JOIN countries c ON ex.country_code=c.country_code
WHERE b.domain NOT IN (SELECT domain FROM banned_domains);

答案 1 :(得分:1)

您可以使用此查询。

select b.domain , e.country_code , c.country_name from base b join  extended e on b.domain = e.domain join countries c on e.country_code = c.country_code and b.domain not in (select domain from banned_domains);

试试此链接http://sqlfiddle.com/#!2/f78ea/1