与没有子查询的同一个表进行比较

时间:2016-05-08 07:43:45

标签: mysql sql

我有一个分支表:

+-------+-----------+--------+
| name  | city      | assets |
+-------+-----------+--------+
| cbi1  | bangalore |   5000 |
| cbi2  | bangalore |   8000 |
| cbi3  | katihar   |  12000 |
| icici | purnia    |  15000 |
| cbi4  | nasik     |  18000 |
+-------+-----------+--------+

我需要找到资产大于班加罗尔任何分支的所有分支。

1 个答案:

答案 0 :(得分:1)

带有子查询的

select name from branch where city !='bangalore' and assets > (select min(assets) from branch where city = 'bangalore') ;

子查询select min(assets) from branch where city = 'bangalore'将为所有banagalore分支提供最低资产价值。 然后将它与表

中的所有剩余分支进行比较

没有子查询

select distinct(b1.name) from branch b1, branch b2 where b1.city !='bangalore' and b2.city ='bangalore' and b1.assets > b2.assets;

不使用子查询,您需要对该表进行自联接。