子查询

13.7.1 转子查询为内连接

select name from country

where code in (select countrycode from countrylanguage);

1.移动CountryLanguage表到From支局

2.这where语句表这代码列和返回的country代码列比较,转换In表达式为明确的比较在country代码列。

select name from country,countryLanguage

where code =countryCode;

为了列出唯一的名字select distinct name from country,countryLanguage where code=contrycode;

13.7.2 转换子查询到外连接

select name from country where code not in(select coutrycode from countryLanguage);

1.移动countrylanguage到from语句和使用left join

2.where语句比较代码列和子查询的代码列进行比较。转换In表达式到一个明确的在country代码列在from语句

3.在where语句,限制输出到这些行有NULL

select name from country left join countrylanguage on code=contrycode where countrycode is null;

也可以转换为右外连接

select from countrylanguage right join country on countrycode =code where countrycode is null;

13.8 使用子查询在updates中

create table NACities select * from city where countryCode in (select code from country where continent ='North America');

DELETE from NACities where CountryCode IN(select code from country where lifeExpectancy <80);

注意:MYsql does not allow a table that is being updated in the outer query to be selected from in any subquery of the statement.如下:

delete from NACities where ID IN (select ID from NACites where Population <500);You can't specify target table 'NACities' for update in FROM clause


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值