use selection from table1 to select from table2

  • Can someone please help me with this SQL. I'm trying to use a field from some selected rows in one table to select rows in another table.

    Heres the SQL i'm trying and the error:

    use pba2

    go

    SELECT Contact_ID, Student_ID, Contact_txMail

    FROM dbo.Contact

    where Student_ID in ( select Enroll_status, Student_ID

    from dbo.Student_Info

    where Enroll_status = 'true');

    Msg 116, Level 16, State 1, Line 5

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    Thank You

  • john 29577 (1/7/2014)


    Can someone please help me with this SQL. I'm trying to use a field from some selected rows in one table to select rows in another table.

    Heres the SQL i'm trying and the error:

    use pba2

    go

    SELECT Contact_ID, Student_ID, Contact_txMail

    FROM dbo.Contact

    where Student_ID in ( select Enroll_status, Student_ID

    from dbo.Student_Info

    where Enroll_status = 'true');

    Msg 116, Level 16, State 1, Line 5

    Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

    Thank You

    To use an in query you can only have 1 column return back e.g.:

    use pba2

    go

    SELECT Contact_ID, Student_ID, Contact_txMail

    FROM dbo.Contact

    where Student_ID in ( select Student_ID

    from dbo.Student_Info

    where Enroll_status = 'true');

    Alternatively you may want to perform a table join for the two tables if you need other columns from the Student_Info table.

  • SELECT Contact_ID, Student_ID, Contact_txMail

    FROM dbo.Contact

    where Student_ID in ( select Enroll_status, Student_ID

    from dbo.Student_Info

    where Enroll_status = 'true');

    So you only want a single record from Contact for each StudentID?

    SELECT Contact_ID, Student_ID, Contact_txMail

    FROM dbo.Contact

    WHERE Student_ID IN (SELECT StudentID

    FROM Student_Info WHERE ENroll_Status='True');

    When you use a subquery (Students that are enrolled), you only return the join column (studentID).

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply