我有一个国家数据库和另一个名为区域的表.
Countries
[id, name, status (1 enabled 0 disabled) ]
Zones
[id, name, country_id]
Run Code Online (Sandbox Code Playgroud)
我使用以下查询来匹配所有国家/地区的区域.
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
Run Code Online (Sandbox Code Playgroud)
所以基本上简而言之,区域是状态,输出就是这样.
+-----------------------------------------------------+----------+--- -----------------------------------------+------------+----------------+
| 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
Run Code Online (Sandbox Code Playgroud)
输出很大,粘贴在这里,所以只显示在结果的末尾
我想将国家/地区的状态更新为0,其中没有区域.知道我怎么能通过mySQL做到这一点?
你可以not in像这样使用:
update Countries set status=0 where id not in (select distinct country_id from Zones )
Run Code Online (Sandbox Code Playgroud)