MySql inner join results as IF condition -
i'm trying use results of inner join condition of if whilst using results in next query. i'm unsure of syntax or how work.
create definer=`root`@`localhost` procedure `addproject`(pname text, uid int) begin /*check if project exists */ if exists (select 1 projects projects.name=pname) begin /* check if user not assigned project*/ select projects.project_id projects inner join assigned_users on assigned_users.project_id=projects.project_id , assigned_users.user_id=uid , projects.name=pname; /*if user not assigned project assign them */ end; /* add new project , assign user project if project doesn't exist */ else begin insert projects(name) values(pname); set @newprojectid = last_insert_id(); insert assigned_users values (@newprojectid,uid); end; end if; end
this want use if condition:
select projects.project_id projects inner join assigned_users on assigned_users.project_id=projects.project_id , assigned_users.user_id=uid , projects.name=pname;
this checking whether specific user assigned specific project. if true want use project id returned condition in code part of if. can store results of condition in variable use in code block of if ?. run query again if condition true strikes me inefficient. appreciated.
Comments
Post a Comment