Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I'm wondering if any of you can help me here...

I have two tables:

Table 1 Columns:

|Event ID|User ID|Event Name|
------------------------------

Table 2 Columns:

|User ID|Username|Password|Display Name|
------------------------------------------

Basically I'm trying to select everything from Table 1, but in the results, I need to show the Username rather than the User ID, and I know I'd have to use an INNER JOIN query to select the Username from Table 2.

I just can't seem to get the syntax right...

Regards,
Tom.
Posted

If you don't want to use alias name than try this.
SQL
SELECT TABLE1.EVENTID, TABLE1.EVENTNAME, TABLE2.USERNAME FROM TABLE1  
INNER JOIN TABLE2 ON TABLE1.USERID = TABLE2.USERID;

Suggestion:
It is better to use alias name so that there is no need of write the whole table name.
 
Share this answer
 
Hello

It's so simple, check below query it will help to you

SQL
Select T1.EventID, T1.EventName, T2.UserName from Table1 T1 Inner join Table2 t2 on T1.UserID = T2.UserID


also find more about the join into below links

http://www.w3schools.com/sql/sql_join.asp[^]

Thanks
Gaurav Dhol
 
Share this answer
 
Comments
thomasriley 28-Aug-12 5:15am    
Hi Dhol and thankyou for your reply, at the FROM part of your answer, you have put "Table 1 T1". What do you mean by "T1"? Or would I put the first table name twice?
bhagirathimfs 28-Aug-12 5:30am    
Here T1 is the alias name of table one[Table1].
If you remove that alias names that it will show error that
"user_id in field list is ambiguous"
because the compiler can't distinguish between the two table's user_id.

So alias name is give to each table.
bhagirathimfs 28-Aug-12 5:31am    
Go through the give link by MR.Dhol. It is a very good site to learn SQL join
Dhol Gaurav 28-Aug-12 5:58am    
Thanks bhagirathimfs
Dhol Gaurav 28-Aug-12 6:00am    
thomasriley please go throw w2school link, it is a very good tutorial with example, after complication of tutorial you are able to resolve this type of problem by your self
No Issues!!!
SQL
Select Table1.EventID, Table1.EventName, Table2.UserName from Table1 Table1 Inner join Table2 Table2 on Table1.UserID = Table2.UserID


Regards
~Karthik~
 
Share this answer
 
v2
Comments
thomasriley 28-Aug-12 5:34am    
I'm doing that and I'm getting a MYSQL syntax error:

/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.username FROM events events INNER JOIN tbl_users tbl_users ON (events.user_' at line 1 */

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900