Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

My question is related to SQL. I need to pull values from two different tables. I am not successful in doing that. I am able to join the two tables with only a single condition but not many values which using IN. Here's my current code:

select
     CP.ID               "Control Point ID"
   , CP.Station          "Station"
   , CP.Station_Series   "Station_Series"
   , SS.Beg_station      "Begin Station"
   , SS.End_Station      "End Station"
   , CP.Status           "Status" 
 from Control_Point CP, Station_Series SS
 where CP.Station_series = SS.ID;

This gives me all the values based on a JOIN but I need only those records which have a value in CP.ID. Something like where CP.ID in (1, 2, 3).

share|improve this question

closed as off topic by Mark Storey-Smith, RolandoMySQLDBA, StanleyJohns, Max Vernon, Paul White Apr 5 '13 at 23:39

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Why is this tagged sql-server? –  Aaron Bertrand Apr 5 '13 at 10:43
    
I thought this is a typical SQL question which folks with SQL Server experience will also have an idea about. –  Naresh Aligeti Apr 5 '13 at 11:22
3  
No, please don't do that. MySQL and Postgres folks might have an idea too, doesn't mean you should tag with a bunch of inappropriate RDBMS tags. –  Aaron Bertrand Apr 5 '13 at 11:35
    
Sorry then, was trying to delete the unnecessary tag but is deleted already. –  Naresh Aligeti Apr 5 '13 at 13:17
add comment

2 Answers 2

That's easy, just add an AND clause to your query:

select
     CP.ID               "Control Point ID"
   , CP.Station          "Station"
   , CP.Station_Series   "Station_Series"
   , SS.Beg_station      "Begin Station"
   , SS.End_Station      "End Station"
   , CP.Status           "Status" 
 from Control_Point CP, Station_Series SS
 where CP.Station_series = SS.ID
  and CP.ID in (1, 2, 3)
share|improve this answer
    
Worked!! Thank you –  Naresh Aligeti Apr 5 '13 at 20:51
add comment

From what you are describing it seems like you are attempting to display all records from Station_Series along with matching records from Control_Point. If that is what you are attempting, perhaps the following makes sense?

SELECT
     CP.ID               "Control Point ID"
   , CP.Station          "Station"
   , CP.Station_Series   "Station_Series"
   , SS.Beg_station      "Begin Station"
   , SS.End_Station      "End Station"
   , CP.Status           "Status" 
 FROM Station_Series SS
    INNER JOIN Control_Point CP ON SS.ID = CP.Station_series;

This will only return records that have a value in CP.ID, since the join will exclude NULL CP.ID values. If you are looking for specific values of CP.ID then you could add a WHERE clause, like:

WHERE CP.ID IN (1, 2, 3)
share|improve this answer
    
Thanks Max, worked fine!! –  Naresh Aligeti Apr 5 '13 at 20:53
    
My pleasure! Would you mind marking this as the answer for future visitors? –  Max Vernon Apr 5 '13 at 21:09
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.