I have a table named as Team_table having a column Team_Id as a primary key, and 4 different table are Team_Member, Team_Project, Team_Account, Team_link also contains this Team_Id.
Before deleting a team from Team_table, I have to check its reference in all 4 tables. if this Team_Id found in any one table, user cant delete it.
I made following query but its not working-
select count(Team.Team_Id)
from Team
join Team_Project on Team.Team_Id = Team_Project.Team_Id
right join Team_Member on Team.Team_Id = Team_member.Team_Id
right join Team_link on Team.Team_Id = Team_link.Team_Id
right join Team_Account on Team.Team_Id = Team_Account.Team_Id
where Team.Team_Id = 2
But its always giving me 0, while this team_Id is available in all tables.
Please suggest me right solution
A group of
right joinends up empty the last table is empty. A group ofleft joinends up empty if the first table is empty. But a group offull joinwill return any matching rows. So consider replacing yourright joinwithfull join.As another option, I find it clearer to use
not existsclauses instead ofjoinfor this kind of query:Per your comment, to retrieve the count: