vendredi 24 juin 2016

SELECT rows WHERE NOT EXISTS condition from 2 tables

I have 2 tables, named SectionAccess and AdminConsole respectively. SectionAccess contains the fields User Access, User ID and Link. 1 User ID can have a few Links. AdminConsole has the fields Link, Project ID (PPMID), Application ID (EPRID) and Project Name. Each Link has a unique Project.

I'm creating a webpage using Razor where the user can key in his User ID, and the webpage will show the projects that are currently under that User ID in a table. The SQL query I did for that is:

SELECT SectionAccess.Link, SectionAccess.UserID, AdminConsole.PPMID, AdminConsole.EPRID, AdminConsole.Proj_Name FROM SectionAccess 
INNER JOIN AdminConsole ON SectionAccess.Link=AdminConsole.Link 
WHERE SectionAccess.UserID like ('%" + Request["UserID"] + "%');

This works great. Now I'm adding another table below this table that will display the projects that are NOT tied to that User ID. I did this, but it doesn't return anything.

SELECT Link, PPMID, EPRID, Proj_Name FROM AdminConsole
WHERE NOT EXISTS 
(SELECT SectionAccess.Link, SectionAccess.UserID, AdminConsole.PPMID, AdminConsole.EPRID, AdminConsole.Proj_Name FROM SectionAccess 
INNER JOIN AdminConsole ON SectionAccess.Link=AdminConsole.Link 
WHERE SectionAccess.UserID = '" + Request["UserID"] + "');

What's the problem here?

Aucun commentaire:

Enregistrer un commentaire