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.
| Author |
Topic |
|
hasanali00
Posting Yak Master
207 Posts |
Posted - 2005-09-06 : 10:52:37
|
| I have following dataOrdersorderid= 1, username=u1, companyid=1orderid= 2, username=u2, companyid=1OrderLineorderLineID=1, orderID=1, confirmed=0orderLineID=2, orderID=2, confirmed=0orderLineID=3, orderID=2, confirmed=0Now I want to get all the orders WHERE orderlineid.confirmed=0So I have this query:SELECT * FROM Orders join OrderLineon Orders.OrderID = OrderLine.OrderID where OrderLine.OrderConfirmed = 0and Orders.CompanyID = @CompanyIDHowever, with this query, I am getting:OrderId122But I should only get orderID 1 & 2 Any idea how I can make this work.thanks |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-06 : 10:57:57
|
| Because OrderLine table has two entries for OrderId 2Use Distinct *SELECT Distinct * FROM Orders join OrderLineon Orders.OrderID = OrderLine.OrderID where OrderLine.OrderConfirmed = 0and Orders.CompanyID = @CompanyIDMadhivananFailing to plan is Planning to fail |
 |
|
|
hasanali00
Posting Yak Master
207 Posts |
Posted - 2005-09-06 : 11:02:38
|
| Actually, I have tried to use Distinct, but it still does not work. Does 'Distinct' work for you?? |
 |
|
|
hasanali00
Posting Yak Master
207 Posts |
Posted - 2005-09-06 : 11:48:06
|
| The correct sql is:SELECT Distinct Orders.* FROM Orders join OrderLine |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-08 : 01:23:37
|
| Yes It is better to use Alias names to get records when using joinsMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|