Left join with where clause is not working - sql

I am trying to get all the users in my DB which have or do not have requests of friendship, but if they do have requests of friendship, I do not want the ones where a particular user is part of that request of friendship.
My stored procedure is named getNotFriendsOfUser.
This is the query :
select * from user a left join request b on a.idPerson = b.idRequester
inner join persona c on a.idPerson = c.idPerson
where b.idRequester <> #idPerson
There are no friendship relationships on my database and there are more than 10 users to test, but I do not find WHY this query is always returning an empty set of values.
This does give results btw :
select * from user a left join request b on a.idPerson = b.idRequester
inner join persona c on a.idPerson = c.idPerson
Help?

then use a subquery or derived table with your left join for the results you want to omit and exclude them on the primary key.

Related

Display Interactive Report for different roles sql query

I'm stuck on something, and need some hep with a SQL query. I've created an interactive report with the following SQL query:
SELECT *
FROM FORM f
LEFT OUTER JOIN MANAGER m ON m.MID = f.MID
LEFT OUTER JOIN USERS u ON u.ID = m.ID
LEFT OUTER JOIN MANAGER m2 ON m2.MID = f.MANID
LEFT OUTER JOIN USERS u2 ON u2.ID = c.ID
WHERE lower(u.username) = lower(:session_user_name)
OR lower(u2.username) = lower(:session_user_name);
The logged in user can have different roles in the form. Depending on the role the id of the manager will be set in different columns of the table.
However, all reports for which the user has been entered,regardless of the role in the form, should still be displayed.
With this query, however, I only see one line for which the has been entered as a Manager.
I don't know much about SQL and don't quite understand what the problem is, can someone help here?
Tried with union :
select *
FROM FORM f
LEFT OUTER JOIN MANAGER m ON m.MID = f.MID
LEFT OUTER JOIN USERS u ON u.ID = m.ID
where lower(u.username)=lower(:session_user_name)
union
select *
FROM FORM f2
LEFT OUTER JOIN MANAGER m2 ON m2.MID = f2.MANID
LEFT OUTER JOIN USERS u2 ON u2.ID = m2.ID
where lower(u2.username)=lower(:session_user_name)
Table form:
create table form (FORMID, COLUMN1 varchar2(50), MID NUMBER,MANID NUMBER);
MID and MANID are foreign keys to the table MANAGER
create table manager (MID number, COLUMN1 varchar2(30));
Example:
Here is a table with some sample data:
In the interactive report the manager with the id 15, should see both entries like this
But now he only gets to see one row, the upper row.

SQL Where on different table

SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id

Sql match id's with names from a table that matches with another table

I have a table friend which has two columns with Id's that belong to specific usernames. See example:
Id_Gebruiker is the user and Id_Gebruiker2 is the friend of the user.
The table friend has id's that belong to the table user.
Then that table user has Foreign Keys (column Id_Client) to the table client (referencing the Id_Client column). The client table contains the usernames.
Now I want to get the corresponding usernames that belongs to the Id's that the Friend table contains. So far I got it working when I use only two tables but I can't get it to work with three tables.
Something like this?
select f.Id_Gebruiker, f.Id_Gebruiker2, u.Id_Client, c.Gebruikersnaam
from friend f
join [user] u on f.Id_Gebruiker2 = u.Id
join client c on u.id_client = c.id
EDIT Assuming the names of the users are on the same table (client) and that every friend.Id_Gebruiker corresponds to a client.Id (so there exists a FK relationship), you can find both names with an extra join:
select c.Gebruikersnaam as UserName, c2.Gebruikersnaam as FriendName
from friend f
join client c on f.Id_Gebruiker = c.Id
join [user] u on f.Id_Gebruiker2 = u.Id
join client c2 on u.id_client = c2.id
Here is another answer that you can check and verify at http://sqlfiddle.com/#!3/2b9dd/1.
The main query for getting names for userid and friendid in friends table is as below.
SELECT f1.userid,
f1.friendid,
y.username AS UserName,
x.username AS FriendName
FROM friends f1
INNER JOIN (SELECT DISTINCT f.friendid,
c.clientname AS UserName
FROM users u
INNER JOIN clients c
ON u.id_client = c.id
INNER JOIN friends f
ON f.friendid = u.id) x
ON f1.friendid = x.friendid
INNER JOIN (SELECT DISTINCT f.userid,
c.clientname AS UserName
FROM users u
INNER JOIN clients c
ON u.id_client = c.id
INNER JOIN friends f
ON f.userid = u.id) y
ON f1.userid = y.userid;

SQL QUERY data reading

I have two table country and Users. I want to view the country name which have user disabled. So i wrote query for this.
SELECT DISTINCT cntr_id,cntr_name FROM
(SELECT COUNTRY.cntr_id, COUNTRY.cntr_name, USERS.user_enabled,
USERS.user_name, USERS.user_id
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
)
AS TAB where user_enabled = 0
My questions are :
Is this inner query?
will the query fetch all the countries (include user enabled) from the database before running outer query?
Is there any other method to select?
Yes it's an inner Query and will fetch what you need, but you don't need your outer SELECT query. You could just do it like:
SELECT DISTINCT COUNTRY.cntr_id, COUNTRY.cntr_name
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
WHERE USERS.user_enabled = 0
Yes, that is inner query.
Yes, inner query will fetch all the countries (including user enabled). Outer query will remove those records later.
You don't have to use outer query for this.
SELECT DISTINCT C.cntr_id, C.cntr_name
FROM COUNTRY C INNER JOIN
Users U ON C.cntr_id = U.cntr_id
WHERE U.user_enabled=0
OR
SELECT C.cntr_id, C.cntr_name
FROM COUNTRY C INNER JOIN
Users U ON C.cntr_id = U.cntr_id
WHERE U.user_enabled=0
GROUP BY C.cntr_id, C.cntr_name
To answer your question,
Yes It is inner and outer two queries.
The inner query will fetch the all rows which satisfy the join condition.
Yes there is other method to select where you can directly apply the filter in inner query and remove the outer select.
SELECT COUNTRY.cntr_id, COUNTRY.cntr_name, USERS.user_enabled,
USERS.user_name, USERS.user_id
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
WHERE
USERS.user_enabled = 0

How to make this join with a TSQL query?

I have a table called USERS that has a foreign key to the table GROUPS (a user can pertain to one or none GROUPS). The table USERS also contains a column ISDELETED (a char column with T or F).
I need a query to retrieve all the GROUPS and all the USERS that are not deleted, if all the users in a GROUP are deleted or no users are defined I need the query to return NULL for that GROUP.
I tried with the following query:
SELECT GROUPS.*, USERS.*
FROM GROUPS INNER JOIN
USERS ON GROUPS.ID = USERS.GROUPID
WHERE USERS.ISDELETED = 'F'
But this query does not returns the groups that are empty. SQL and me are not the best friends in world, some help will be great, thanks.
If you want all the groups, regardless of a match in the users table, you should use a left outer join:
SELECT GROUPS.*, USERS.*
FROM GROUPS
LEFT OUTER JOIN
USERS
ON GROUPS.ID = USERS.GROUPID AND USERS.ISDELETED = 'F'
You should just need to do a left outer join -
SELECT GROUPS.*, USERS.*
FROM GROUPS LEFT OUTER JOIN
USERS ON GROUPS.ID = USERS.GROUPID
WHERE USERS.ISDELETED = 'F'
Here's a reference I like to use to remind myself of the differences in sql joins.
You need to use the LEFT OUTER JOIN operator instead of the INNER JOIN.