Left join query in MySQL two table difference set

Source: Internet
Author: User
Tags join openid

Today, while doing a mini-letter wall lottery program, I encountered a problem, I need to query the difference set of scale, the business situation is this

A table is used to save lottery users (www.111cn.net a person may have more than one piece of data), while the other table is saved by the winning users, I need to report the winner of the user to find out, just start with a where to do a multiple table association query, But it turns out that when there's no data in the jackpot list, it's easy to use the left JOIN.

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

Left JOIN query, look for the same, if not the lottery table will be directly judged with null fields can filter the data, and then get the necessary data.

Example

Environment: A/b Two tables are related tables. Association field, PID. Table A is the main table with more data than table B. Now query a table in existence, B table does not exist in the data

The code is as follows

SELECT * from ' a ' a '-left JOIN ' B ' B ' in A. ' PID ' =b. ' pid ' where B. ' PID ' is NULL and LENGTH (A. ' pid ') <10

Describe the knowledge used in the following SQL:

1. The table on the left side of join On:left join is the main table, and each piece of data in the primary table is displayed. Null if no data is in the table on the right

2, length to calculate string lengths

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.