Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table having two fields say ad_id and tag. I want to filter values on the basis of tag field by AND operator.

ad_id tag
1 nokia
2 samsung
1 windows
Here I want to find out values having tag value nokia or samsung but only windows.
Posted
Comments
King Fisher 17-Nov-14 6:10am    
what have you tried?
where you stuck ?
King Fisher 17-Nov-14 6:12am    
use OR operator :)
Vipin Mishra 17-Nov-14 6:18am    
The 'or' and 'and conditions are dynamic. Means I want to implement filter on my existing classified website where I had implemented tags for field like brand(nokia,samsung,sony etc), os(windows,android,symbiane,.etc),network(2g,34,4g,etc). So I want to implement filter as a user can search for mobile which is either nokia or samsung but having os only windows and network 2g or 3g.
Shweta N Mishra 17-Nov-14 8:35am    
Vipul are you having relation between devices and their OS.
as in i see Nokia ad_id is 1 and so is windows. Is that what relates to them ?

HI,

TRY LIKE THIS
SQL
DECLARE @TEMPTB TABLE(
ID INT IDENTITY(1,1)
,BRAND VARCHAR(50)
,OS VARCHAR(15)
,NETWORK VARCHAR(20)
)
INSERT INTO @TEMPTB
(BRAND,OS,NETWORK )
SELECT 'NOKIA','WINDOWS','2G'
UNION ALL
SELECT 'SAMSUNG','WINDOWS','2G'
UNION ALL
SELECT 'SAMSUNG','ANROID','3G'
UNION ALL
SELECT 'HTC','WINDOWS','3G'
UNION ALL
SELECT 'NOKIA','ANROID','2G'


SELECT * FROM @TEMPTB


SELECT * FROM @TEMPTB  WHERE OS ='WINDOWS' AND (BRAND = 'NOKIA' OR BRAND='SAMSUNG' OR BRAND='HTC') AND (NETWORK='2G' OR NETWORK = '3G')
 
Share this answer
 
how about this.

SQL
select * From table where   tag in ('nokia','samsung','samsung')
 
Share this answer
 
v2
Comments
/\jmot 17-Nov-14 7:40am    
is this Query correct??
/\jmot 17-Nov-14 7:48am    
i think..
it'll never return any result :P
Shweta N Mishra 17-Nov-14 8:33am    
I assume that user is going to select multiple tag for device e.g. Nokia and Samsung
and for platform he is going to select windows .

He needs to work with his data , thats what i missed.
Shweta N Mishra 17-Nov-14 8:39am    
updated the query
/\jmot 17-Nov-14 14:41pm    
yes, thats it.
SQL
select *From table where   tag ='nokia' or tag = 'samsung' ;


refer this :
AND vs OR

http://www.w3schools.com/sql/sql_and_or.asp[^]
 
Share this answer
 
v2
Comments
Vipin Mishra 17-Nov-14 6:22am    
The 'or' and 'and conditions are dynamic. Means I want to implement filter on my existing classified website where I had implemented tags for field like brand(nokia,samsung,sony etc), os(windows,android,symbiane,.etc),network(2g,34,4g,etc). So I want to implement filter as a user can search for mobile which is either nokia or samsung but having os only windows and network 2g or 3g.
King Fisher 17-Nov-14 6:36am    
so the tags like this
[android],[samsung],[3G]

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