In mysql, left join queries the difference sets of two tables.

Source: Internet
Author: User
Tags openid

I encountered a problem when I was doing a wall lottery program today. I need to query the difference set of the table. The business situation is like this.

One table is used to store lottery users (www.111cn.net may have multiple pieces of data), and the other table stores the winning users. I need to report to the users who have won the prize and find out the winning users, at the beginning, I used the where clause to perform multi-table JOIN queries. However, I found that I could not find any information when there was any data in the winning table. It was very easy to use LEFT JOIN instead,

The Code is as follows:  

Desc select *
FROM 'enet _ wall_list 'AS l
Left join 'enet _ wall_lottery 'AS lottery ON l. openid = lottery. openid
WHERE l. weid = 63
AND lottery. id IS NULL
Group by l. openid

The left join query is used to search for equal values. If there is no lottery table, the NULL field is used to directly filter the data and obtain the required data.

Example

Environment: Tables A and B are associated tables. Associated field, pid. Table A is the primary table with more data than table B. The data in table A and table B does not exist.

The Code is as follows:  

SELECT * FROM 'A' a left join 'B' B ON. 'pid '= B. 'pid 'where B. 'pid 'is null and LENGTH (. 'pid ') <10

The following describes the knowledge used in the preceding SQL statements:

1. left join on: The table ON the left of LEFT join is the primary table. Each data entry in the primary table is displayed. If no data exists in the table on the right, it is null.

2. LENGTH: calculates the LENGTH of a string.

Related Article

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.