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.