Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Question regarding multiple Where statements.

Author  Topic 

larryg003
Starting Member

18 Posts

Posted - 2010-06-10 : 16:40:14
Hi Gang,

I just got a copy of SQL and I am running through the tutorials for it online.

2 quick things:
1.) How do I match two columns and make sure that they are both distinct.

I have 10 columns and two columns I care about: A & B.
I column A everything is distinct and in B only some of them are distinct.

I only want the ones where both Column A and Column B are distinct.
I tried:

select * from table
where "A"=distinct and
where "B"=distinct;

and it didn't work. Any Ideas?

2.) How do you also make double filters using where statements?
I have column which has a bunch of tags (most having 4 or more tags in one field in different orders): TYU, BUI, HSU, etc.

I want to make it so I can search by the one's I want

I want to filter them so that I can filter by two or three tags at a time.

I tried:
Select * from Table
where "C" like '%TYU%' and '%BYU%'

and it didn't work.
Do I need to run subquerries to fix this problem or can I do it in 1 statement?

THANK YOU SO MUCH!! I AM ETERNALLY GRATEFUL!

Kristen
Test

22859 Posts

Posted - 2010-06-10 : 17:28:01
1) I don't understand what you mean by Distinct, but to just correct your syntax would be somethign like this:

select * from table
where [A] = SomeColumnOrValue
and [B] = SomeColumnOrValue


2) You should not put multiple values in a single column; you should have another table with two colums: the ID of the first table, and another for ONE of the tags. So your main record would have 4 associated records, if there were 4 matching TAGs

You can do

Select * from Table
where [C] like '%TYU%' OR [C] LIKE '%BYU%'

but it will be incredibly inefficient compared to

Select *
from Table AS T
JOIN TagsTable AS TAG
ON TAG.id = T.id
where TAG.TagField IN ('TYU', 'BYU')


also

where [C] like '%TYU%'

will match xTYU, TYUx, xxtYUXXX and so on.
Go to Top of Page

larryg003
Starting Member

18 Posts

Posted - 2010-06-10 : 19:10:04
Dear Kristen,

Thank you so much for your help.
Q1 I phrased badly...i will try and formulate it better next time '
and in a more cohesive manner.

As for Q2, I tried it out and it works great. I have a huge database worth of material and I need to use method 1 that you mentioned to go through it. I didn't design it and this is the way it was given to me.

Thank you again,

-Larry
Go to Top of Page
   

- Advertisement -