Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linking data from 2 tables

Status
Not open for further replies.

xelianx

Programmer
Jul 11, 2006
1
US
Hi,

I'm relatively new to SQL and databases in general, and I have a question about linking data between 2 tables. I have one table which lists department names and each department has its own id (primary key) it looks like this:

id int8
name varchar(50)

I have another table which stores data from user input. One of the columns is the department id.

task_id int8
.
.
.
department_id int8 <- this would map to the id column in the previous table.


Is it possible to link the department id column in the user input table to the other table so that when I run a query, the department name will be returned instead of the id?

Thanks!

-Mike
 
Hi

Yes or no. Yes, they could be linked, which in SQL is called [tt]join[/tt]. No, if you want this to happen automagically each time without your contribution.
Code:
[b]select[/b]
department.name,userinput.*

[b]from[/b] department
[b]inner join[/b] userinput [b]on[/b] userinput.department_id=department.id
If you need it many times, then better create a view
Code:
[b]create view[/b] userinput_with_name [b]as[/b]
[b]select[/b]
department.name,userinput.*

[b]from[/b] department
[b]inner join[/b] userinput [b]on[/b] userinput.department_id=department.id
Then when you need data [tt]select[/tt] from the view instead of the table :
Code:
[b]select[/b]
*

[b]from[/b] userinput_with_name
Note : I do not like views and I do not suggest to use them. I mentioned this possibility just as information.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top