找出那些 没有被 id = 2 的客户 推荐 的客户的姓名。
建表插入数据:
Create table If Not Exists Customer (id int, name varchar(25), referee_id int)
Truncate table Customer
insert into Customer (id, name, referee_id) values ('1', 'Will', 'None')
insert into Customer (id, name, referee_id) values ('2', 'Jane', 'None')
insert into Customer (id, name, referee_id) values ('3', 'Alex', '2')
insert into Customer (id, name, referee_id) values ('4', 'Bill', 'None')
insert into Customer (id, name, referee_id) values ('5', 'Zack', '1')
insert into Customer (id, name, referee_id) values ('6', 'Mark', '2')
思路分析:
找没有被id=2的客户推荐的客户姓名,查找id!=2或者id is null的即为所求。
画图分析:
代码实现:
select name from customer where referee_id!=2 or referee_id is null;