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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to SQL

Status
Not open for further replies.

redoctoberz

IS-IT--Management
Apr 8, 2002
2
US
Hi,
I am very very new to SQL. I need some quick help. This is what my problem is. I have two tables. One table has one of the column as user_id. The other table has a mapping from user_id to user_name. Now, I want every column in first table but replace the user_id with user_name.
How do I right this statement?
Thanks.
 
select secondTAble.username, firstTable.c1,firstTable.c2
from firstTable,secondTable
where firstTable.user_id = secondTable.user_id
 
SELECT tableB.user_name, tableA.*
FROM tableA
JOIN tableB ON tableA.user_id = tableB.user_id
 
You need a join to the other table. You also should list out all the columns you want from the first table and then the column you want from the second table in the select statement. Assume Table one has the columns, UserID, Col1, Col2. The staement to get what you want would be:

Select col1, col2, UserName from table1 t1 join table2 t2 on t1.UserID = t2.UserID

This statement assumes that you only want records which are in both tables. The t1 and t2 are aliases for the table names to make further references to them shorter to type. Wihoout the aliases the statement looks like:
Select col1, col2, UserName from table1 join table2 on table1.UserID = table2.UserID

If any of the column names are repeated in both tables you will need to specify which one. For instance if you also wanted to show the UserID you would need to specify the table you want it from.

the select statement then becomes:
Select t1.UserID, col1, col2, UserName from table1 t1 join table2 t2 on t1.UserID = t2.UserID
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top