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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 either or equivalent in sql

Author  Topic 

kandy
Starting Member

15 Posts

Posted - 2006-10-04 : 14:38:45

What is the equivalent for a either 'or' query in sql.ie.
select * from table3 which satisfies <either condition1 frm table1>
<or condition 2 from table 2>

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-10-04 : 14:51:59
Did you try using OR?

SELECT * FROM SomeTable
WHERE Column1 = 1 OR Column2 = 1

Tara Kizer
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-04 : 18:05:05
Looks like you're using three tables so you have to join them first

SELECT t1.* 
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t2.someid = t1.someid
LEFT OUTER JOIN table3 t3 ON t3.someid = t1.someid
WHERE t2.column = something OR t3.column = something


I used OUTER joins because the fact you want either or suggest that there won't always be a row in table2 or table3 to match to the row in table1.
Go to Top of Page

kandy
Starting Member

15 Posts

Posted - 2006-10-05 : 14:01:08
i tried using tht query..doesnt seem to work..
i need
select column1,column2 from table 1
t1 and t3 mus be joined on column a
t2 and t3 mus be joined on column b in t4
where column='' from t2 or column ='' from t1
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-05 : 15:15:15
You'll need to post a better example of what you need. Suddenly you say "in t4", I have no idea what that means, you didn't even have a t4 in your original question.

Post sample data from the three/four tables and explain what "in t4" means.
Go to Top of Page

kandy
Starting Member

15 Posts

Posted - 2006-10-05 : 16:22:40
i have 4 tables.
orders(order_num ,order_date)
part(part_num,description)
customer(customer_name,customer_num)
order_line(part_num,order_num)

I need to list the order number and order datefrom orders table for each order that was either placed by Johnson's Department Store(i.e customer_name in customer table)
or that contains an order line for a Gas Range(gas range is the value for description in the part table)

customer and orders table join on customer_num
orders and order_line table join on order_num
part table and order_line table join on part_num
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-10-05 : 17:39:39
[code]SELECT o.order_num, o.order_date
FROM orders o
LEFT OUTER JOIN customers c ON c.customer_num = o.customer_num
LEFT OUTER JOIN order_line ol ON ol.order_num = o.order_num
LEFT OUTER JOIN part p ON p.partnum = ol.part_num
WHERE c.customer_name = 'Johnson''s Department Store'
OR p.description = 'Gas Range'[/code]
Go to Top of Page
   

- Advertisement -