Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have never written anything but basic SQL queries. I am trying to join two tables and I am getting this error :

Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'UserId'.

This is my SQL query :

SELECT UserName, UserId
FROM aspnet_Users 
JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId

And this is my database diagram:

enter image description here

What is wrong with my query?

Also could anyone tell how should I structure this query in order to get the UserName and RoleName?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

You must specify the table name when using multiple tables since UserId appears in both tables.

Assuming you want UserName and UserId from aspnet_users:

SELECT aspnet_Users.UserName, aspnet_Users.UserId
FROM aspnet_Users JOIN aspnet_UsersInRoles 
ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...