Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fairly Easy: Multiple Column Queries

Status
Not open for further replies.

JustSomeDude

IS-IT--Management
Mar 5, 2003
7
US
Hi all --

I've tried searching around for the answers to my fairly simple questions, but can't seem to find them. I apologize if I've missed some really obvious thread and am re-visiting an old topic.

That said;

Is it possible to write a query that will consider conditions on more than one column? I have tried messing with the SQL, but for some reason it gives an error.

For example, I have a table with two columns, "Animal" and "Size". I want to be able to display rows where the Animal is like "bear" or "Tiger" or if the Size is like "Medium". My query has an OR condition across two columns. I can't seem to get this to work. I suppose I can do some stuff with CREATE and APPEND tables, but I am trying to avoid that.

Here is what I am trying to get:

ANIMALS SIZE
------- ----
Bear Large
Bearcat Small
Tiger Large
Cat Small
Ocelot Small
Chimp Medium

With my query: "Give me rows where ANIMALS is like "Bear*" or SIZE="Medium", I want to be returned "Bear", "Bearcat" and "Chimp". Unfortunately, I can't seem to make the OR condition include both columns ANIMALS and SIZE...

Any ideas that don't involve two queries and a couple CREATE and APPEND tables?

Thanks for any insight!
 
select animals, size from zoo where (animals like "bear*" or animals = "chimp") and size = "medium"

or

select animals, size from zoo where (animals like "bear*" and size = "medium") or animal = "chimp"


not sure exactly what you wanted but either way this should work.

p.s. zoo is the table name
 
Open the Query in Design view. For the Animals column, on the first Criteria line, type in
LIKE [Enter animal to display]

On the same line, in the Size column, type in
[Enter Size required]

On the next criteria line, type in
Animals column LIKE [Enter animal to display]
Size column [Enter Size required]

This will give you the (Animal AND Size) OR (Animal AND Size) conditions.

If you need the SQL code for this, simply open the query in SQL View (VIEW|SQL VIEW on the menu) and the code will be displayed. You can copy and paste from this view.

HTH
Lightning
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top