Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   MySQL (https://www.askmehelpdesk.com/forumdisplay.php?f=442)
-   -   How to reference multiple tables in a SQL Server Query/select Statement (https://www.askmehelpdesk.com/showthread.php?t=497228)

  • Aug 11, 2010, 01:49 PM
    ashley velez
    How to reference multiple tables in a SQL Server Query/select Statement
    When running a select statement select field1, field2, field3, field4 from table1

    The values for field2 and field3 are a "code"...

    The value of the code in field2 is in table2, and the value for the code in field3 is in table3

    Example: Field2=16 , In the employee table, There is a column employee_id. Where employee_id=16, first_name_x= Jane , Last_name_x= Doe

    So instead of having the value for field2=16, Id rather the output be Doe, Jane

    Another Example: Field3=AA ; In the table code_category, There is a column code_c. Where code_c=AA , description_x= Available for Adoption

    So instead of having the value for field3=AA, Id rather the output be Available for Adoption

    So, I would like to run a select statement that pulls up the values for field1-4... but substitutes the codes in field2&3 for the description...

    I hope you follow, sorry if I made this sound more confusing than it needed to be.


    Not sure if it matters, but I am doing this in SQL Server Management Studios.
  • Aug 24, 2010, 11:14 AM
    rpray2007
    I think you want to use a JOIN statement. http://en.wikipedia.org/wiki/Join_%28SQL%29 will explain how this works. Basically, join statements allow you to lock down the value of one column in a certain field in one table while selecting rows given that value in other tables using a WHERE clause.

    Alternatively: If you are not concerned about speed, you can just throw everything just list all tables (comma separated) and use the where clause where you can specific table1.field1 = table2.field2 (for example)

    One more alternative is to use sub selects:
    SELECT c1 from t1 WHERE f1 in (SELECT f FROM t2 where y=w);

    Hope one of the above helps.


  • All times are GMT -7. The time now is 12:57 AM.