Summary of Leetcode-Mysql topics and knowledge points (584. Looking for user recommenders)

Computer Xiaobai QAQ, because I want to find a few summer internships, I have filled the members and want to focus on the mysql part of leetcode. Write this series of blog posts to communicate with you, and we will continue to update some of the prepared questions in the future. Welcome to communicate with you, and ask the big guys to spray QAQ. Because I have taken many detours as a beginner, I will try my best to write in detail. If you can help future friends, please leave a message to encourage me. Hahahaha.

584. Find the user's referrer

Idea: Referrer ID has three statuses: 2, other id, null. It is not 2, as long as the filter is not 2 and null, the condition is satisfied.

There are no new knowledge points. Note that it does not mean that it can be represented by "<>".

Code:

select name

from customer

where referee_id<>2 or referee_id is null

On the other hand, some friends may say why I can’t find out 2 first, and then filter the conditions in the subquery. It’s okay, but we should pay attention to one situation, that is, duplicate names. I have made the same mistake before. The code is as follows :

select name from customer where name not in (select name from customer where referee_id =2 )

People with the same name and different IDs have different IDs corresponding to one that is not 2, and there will be problems. After all, the ID is the primary key.

Guess you like

Origin blog.csdn.net/weixin_43167461/article/details/113237417