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