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 2008 Forums
 Transact-SQL (2008)
 i'm involved with this select statements

Author  Topic 

hazem_world
Starting Member

3 Posts

Posted - 2010-09-14 : 10:28:00
plz help me to choose '999' only from the two select statements as the "station_id" is 3 and 5




select trains.name , journey.station_id , journey.time
from trains left join journey on trains.id=journey.trainId
where station_id=3

select trains.name , journey.station_id , journey.time
from trains left join journey on trains.id=journey.trainId
where station_id=5


7azooom

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 10:44:02
select trains.name , journey.station_id , journey.time
from trains left join journey on trains.id=journey.trainId
where journey.station_id in (3,5)
and trains.name = '999'



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

hazem_world
Starting Member

3 Posts

Posted - 2010-09-14 : 10:55:59
thx webfreed
i don't want trains.name='999' i've just type it to illustrate
i want sql to get all trains that have station_id = 3 and 5
but where journey.station_id in (3,5) get station_id = 3 or 5


7azooom
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 11:06:05
select t1.name , j1.station_id , j1.time
from trains t1 left join journey j1 on t1.id=j1.trainId
where j1.station_id = 3
and exists(select * from trains t2 left join journey j2 on t2.id=j2.trainId where j2.station_id = 5 and t2.name = t1.name)



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

hazem_world
Starting Member

3 Posts

Posted - 2010-09-14 : 11:17:02
oops
thaaaaaaaaaaank you very very very much :)

7azooom
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 11:20:25
You're welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -