Home > database > How in the condition judgment to execute SQL statements
How in the condition judgment to execute SQL statements
Time:09-24
Database with two tables, the Company: the Company, the User: the User Company in the customer table Id UserID name 1 5 xiamen XX trading co., LTD. Shantou 2 6 XX textile co., LTD. 3 5 changzhou XX textile printing and dyeing co., LTD. 4 2 hangzhou XX cloth art co., LTD. 5 3 foshan XX channgzhou 6 5 foshan XX textile co., LTD. 7 2 cotton industry company in foshan XXX User table Id name sysName DepartmentCode 1 XX maggie Chen 00106 2 CAI XX caicai 00104 3 ko XX viline 00102 4 XX Doris liu 00103 5 XX Cindy yu 00106 6 yellow XX Amily 00104
Involves a question of authority, department number (DepartmentCode) as the '00104', is a sales department, the department The sales staff in view, can only see their own customers, Such as the User table '2 CAI XX caicai 00104, The query SQL='select * from the Company left the join the User on the Company. The UserID=User. The id where 1=1 and the User. DepartmentCode=' 00104 '. And the User id=2 ' And in addition to the sales staff, can see all of the customers, do not need to specify the id The query SQL='select * from the Company left the join the User on the Company. The UserID=User. The id where 1=1' and the User. The DepartmentCode! ='00104' Page incoming parameters: @ UserId My problem is that by the incoming parameters, how to combine the above conditions in a SQL execution, by judging whether incoming parameter @ UserId is belong to the sales staff to perform the query results,
I tried several ways: . 1. (the case when the User DepartmentCode='00104'. Then the User id=@ UserId else User. Id<> NULL end) . There is an error, then after can't User id=@ UserId 2. (the User DepartmentCode='00104' and the User. The id=@ UserId) or (User DepartmentCode!='00104' and the User id!=null) After the query results cannot jump or always (User id!=null) 3. (the User id (in the SELECT id FROM the User WHERE DepartmentCode='00104' AND id=@ UserId)) OR (. User id is not null) This paragraph is always judge (User id is not null), input sales staff id also check out all the results 4.
Pretty good, really can't solve this problem, please, I have a look, if it is my way out of the wrong will check it, or there's a better solution on this issue can instruct me, I thanked the here, this is all my points, beg for my disambiguation answer
CodePudding user response:
Put this logic in the application process more convenient, why to calculate in SQL processing logic,
CodePudding user response:
1. Through UserId access to user DepartmentCode 2. Through DepartmentCode different SQL execution