2016-02-07 93 views
3

我有一个国家数据库和另一个表称为区域。加入mySQL更新查询

Countries 
[id, name, status (1 enabled 0 disabled) ] 


Zones 
[id, name, country_id] 

我正在使用以下查询来匹配所有国家与他们的区域。

select 
z.name as state, 
z.id as state_id, 
c.name as country_name, 
c.id as country_id, 
c.status as country_status 
from countries c left join zones z on c.id = z.country_id 

所以基本上总之一个区域是状态和输出是这样的。

+-----------------------------------------------------+----------+--- -----------------------------------------+------------+----------------+ 
| state            | state_id | country_name        | country_id | country_status  | 
+-----------------------------------------------------+----------+--- -----------------------------------------+------------+----------------+ 
| NULL            |  NULL | Christmas Island       |   45 |    1 
| NULL            |  NULL | Puerto Rico        |  172 |    1  
| NULL            |  NULL Isle of Man        |  254 |    1 
| Álava          |  2971 | Spain          |  195 |    1 
| Ávila          |  2976 | Spain          |  195 |    1 
| Évora          |  2656 | Portugal         |  171 |    1 

输出是巨大的粘贴这里,所以只显示在结果

我想更新各国的状态,以0那里是没有区域的末端。任何想法如何我可以通过mySQL做到这一点?

回答

5

可以使用not in这样的:

update Countries set status=0 where id not in (select distinct country_id from Zones) 
+0

由于工作完美。 – limit