我需要帮助!例如,有四个表:cars、users、departments 和 join_user_department。最后一个表用于 M: N 表用户和部门之间的关系,因为某些用户的访问权限有限。我需要获取用户可以访问的部门的汽车数量。表“cars”有一个列 department_id。如果表 join_user_department 没有 user_id 的任何记录,这意味着他可以访问所有部门,并且选择查询必须没有任何条件。我需要做这样的事情:
declare
DEP_NUM number;--count of departments where user have access
CARS_COUNT number;--count of cars
BEGIN
SELECT COUNT (*) into DEP_NUM from join_user_departments where user_id=?;
SELECT COUNT(*) into CARS_COUNT FROM cars where
IF(num!=0)—it meant that user access is limited
THEN department_id IN (select dep_id from join_user_departments where user_id=?);