In MySQL, if is determined based on multiple conditions in where, the project requirements are as follows:
In a friend relationship, there are three roles, namely A, B, and C. The types of friends are dynamic: 1, 2, 3, 4, and 5. A is a required role. If a has a role, you can check the dynamic type 1, 3, and 4 of friends. If B has a role, you can check 2 types of information. If a has a C role, you can check 5 types of roles.
The implementation idea is as follows: first identify all the dynamic data of the friend relationship-"Judge B role to add filter 2 type-" Judge C role to add filter 5 type
The following describes how to implement step aggregation. The added data is not described here.
1. Add a relational table
Copy content from SQL code to clipboard
- --
- -- Table structure 'table'
- --
- Create Table if not exists 'table '(
- 'Uid' int (11) not null auto_increment comment 'follow id ',
- 'User _ id' int (11) not null comment 'user name id ',
- 'User _ follow_id 'int (11) not null comment' user ID to be followed ',
- 'Iscarer' tinyint (2) not null default '0' comment' role a follow friend 1 is 0 No 2 ignore ',
- 'Ishelper 'tinyint (2) not null default '0' comment' Role B follow Master 1 is 0 No 2 ignore ',
- 'Isprovision' tinyint (2) not null default '0' comment' role C follow service provider 1 is 0 No 2 ignore ',
- 'Insert _ time' varchar (20) not null comment' increase time ',
- Primary Key ('uid ')
- ) Engine = MyISAM default charset = utf8 comment = 'user relationship follow table' auto_increment = 21;
2. Add a friend dynamic table
Copy content from SQL code to clipboard
- --
- -- Table structure 'table B'
- --
- Create Table if not exists 'table B '(
- 'Uid' int (11) not null auto_increment comment 'message id ',
- 'Type' tinyint (4) not null comment 'type 1 car owner article 2, follow the article, 3 ask a question, 4, investigate, 5 order information ',
- 'User _ id' int (11) not null,
- 'Event _ message' text not null comment 'dynamic data serialization ',
- 'Insert _ time' varchar (20) not null,
- Primary Key ('uid ')
- ) Engine = MyISAM default charset = utf8 comment = 'friends dynamic table' auto_increment = 3;
3. The dynamic record of friends is as follows:
The SQL statement is as follows:
Copy content from SQL code to clipboard
- Select
- *
- From
- Table A F
- Left join (
- Select
- Type,
- User_id as author_id,
- Event_message
- From
- Table B
- ) E on F. user_follow_id = E. author_id
- Where
- User_id = 275
- And iscarer = 1
- And type is not null
Result 3:
4. Add Role B to judge the SQL statement as follows:
Copy content from SQL code to clipboard
- Select
- *
- From
- Table A F
- Left join (
- Select
- Type,
- User_id as author_id,
- Event_message
- From
- Table B
- ) E on F. user_follow_id = E. author_id
- Where
- If (ishelper = 0, type <> 2, '1 = 1 ')
- And user_id = 275
- And iscarer = 1
- And type is not null
Result 2:
4. Add the C role to judge the SQL statement as follows:
Copy content from SQL code to clipboard
- Select
- *
- From
- Table A F
- Left join (
- Select
- Type,
- User_id as author_id,
- Event_message
- From
- Table B
- ) E on F. user_follow_id = E. author_id
- Where
- If (ishelper = 0, type <> 2, '1 = 1 ')
- And
- If (issupplier = 0, type <> 5, '1 = 1 ')
- And user_id = 275
- And iscarer = 1
- And type is not null
Result 1:
Well, we have achieved the desired effect.
PS: The following where statements can be sorted by index value.
Copy content from SQL code to clipboard
- Where
- User_id = 275
- And iscarer = 1
- And type is not null
- And
- If (ishelper = 0, type <> 2, '1 = 1 ')
- And
- If (issupplier = 0, type <> 5, '1 = 1 ')