Examples of mysql left-connected self-connection

Source: Internet
Author: User
Mysql left-join self-join example joins two tables into a large table according to a public field. The left join is used to traverse the right table based on the table on the left. 1. Lead left join, self join SELECT * FROMtest_visitt1LEFTJOINtest_visitt2ONt1.uidt2.uid result: 2. Problem

Mysql left-join self-join example joins two tables into a large table according to a public field. The left join is used to traverse the right table based on the table on the left. 1. Lead left join, self join select * FROM test_visit t1 left join test_visit t2 ON t1.uid = t2.uid result: 2. Problem

Example of mysql left-connected self-connection

Join is to splice two tables into a large table according to a public field.

The left join is used to traverse the right table based on the table on the left.


1. Introduction

Left join, self join


SELECT * FROM test_visit t1 LEFT JOIN test_visit t2 ON t1.uid = t2.uid

Result:




2. Example:

User Access record:


Problem: I found out that I had watched Hunan TV but not Beijing TV.

Logic: first, I checked Hunan Satellite TV and Beijing satellite TV through the left connection, and then I checked out the users who saw Hunan Satellite TV but did not find the result just now.

SELECT * FROM test_visit WHERE channel = 'hunan satellite TV 'AND uid not in (select distinct t1.uid FROM test_visit t1 left join test_visit t2 ON t1.uid = t2.uidWHERE t1.channel = 'hunan satellite TV' AND t2.channel =' beijing TV ')


Result:


Do you think this is not the best method?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.