Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 4 tables in database

SQL
1. Users Columns(Id, Email, Password, Active)
    2. InformationData Columns(Id, Title, Description, Link)
    3. Filters Columns(Id, Keyword, Active)
    4. LUserToFilter Columns(UserId, FilterId)


Now What I Want Is To Filter Specific Records From InformationData Table In Which Title And Description Does Not Contains Keywords From Filters Table And We Select Those Filters From LUserToFilter Table Based On A Specific User Id.

I Tried This Query But It Only Returns Those Records Which Contains That Filterd Keywords And Not Return The Other Records.

SQL
Create Procedure [dbo].[SP_GetFilteredData]
    @userid int
    as
    begin
    select D.* from Filters f
    inner join LUsersToFilters L on L.FilterId=F.Id
    inner join InformationData D on D.Title like '%'+F.KeyWord+'%' or D.Description like '%'+F.KeyWord+'%'
    where L.UserId=@userid
    end


Any Suggestion Please..
Posted
Comments
creepz03 13-Dec-13 1:44am    
First of all, please stop using capital letters on each word you type.. its kinda hard to comprehend. And second, be more precise as to what you really want to do please?
Hammad 13-Dec-13 1:47am    
Ok i want to show those rows from InformationData that do not contain any of the words from Filters in either their title or description columns
creepz03 13-Dec-13 2:14am    
Ok, I'm going to clarify this.. You want to show all the data from InformationData that does not exist from the Filters(KeyWord)? Is that it?
Hammad 13-Dec-13 2:17am    
I used the word 'contains' not 'exist' that why i use 'like' not '='

try this

SQL
Create Procedure [dbo].[SP_GetFilteredData]
    @userid int
    as
    begin
    select D.* from Filters f
    inner join LUsersToFilters L on L.FilterId=F.Id
    inner join InformationData D on D.Title not /*add not*/  like '%'+F.KeyWord+'%' or D.Description not/*add not*/ like '%'+F.KeyWord+'%'
    where L.UserId=@userid
    end
 
Share this answer
 
Try this:

SQL
SELECT *  FROM InformationData
WHERE NOT EXISTS
(SELECT * FROM Filters INNER JOIN LUserToFilter ON Filters.id=LUserToFilter.FilterId WHERE (InformationData.Title LIKE CONCAT('%',Filters.KeyWord,'%')  OR InformationData.Description LIKE CONCAT('%',Filters.KeyWord,'%')) AND LUserToFilter.userid = @userid)
 
Share this answer
 
Comments
Hammad 13-Dec-13 2:46am    
Work like a charm buddy have a nice day....
Peter Leow 13-Dec-13 2:49am    
Great to know that it helps, you too have a nice day.
Maciej Los 13-Dec-13 2:52am    
+5!
Peter Leow 13-Dec-13 3:10am    
Thank you.
Think solution 1 needs to be modified as below if you are only interested in descriptions where keywords do not appear in either the description or the title.


SQL
--  not in title and not in description
inner join [InformationData]
      on   not [InformationData].[Title] like '%'+[Filters].[Keyword]+'%'
      and  not [InformationData].[Description] like '%'+[Filters].[Keyword]+'%'
 
Share this answer
 
It should actually be a much easier query if you have a more organize table relationship.
Try this..
SQL
select
	d.*
from InformationData d
where d.Title not in (select f.Keyword from Filters f inner join LUsersToFilters l on l.FilterId = f.Id where l.UserId=@userid)
	or d.Description not in (select f.Keyword from Filters f inner join LUsersToFilters l on l.FilterId = f.Id where l.UserId=@userid)
 
Share this answer
 
Comments
Hammad 13-Dec-13 2:28am    
Does not work for me it gives all records of the table not the filtered ones

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900