I have this T-SQL Server 2008 query:
SELECT users.user_id,
course_main.course_name,
course_main.course_id
FROM users,
course_main
INNER JOIN course_application
ON users.pk1 /*(error #1)*/ = course_application.pk1
INNER JOIN course_main /*(error #2)*/
ON course_application.crsmain_pk1 = course_main.pk1
INNER JOIN course_users
ON users.pk1 /*(error #3)*/ = course_users.users_pk1
AND course_main.pk1 = course_users.crsmain_pk1
AND course_main.pk1 = course_users.child_crsmain_pk1
WHERE course_users.role = 'P'
AND ( ( course_main.course_id ) LIKE '%FA2013'
AND ( course_application.application ) = 'alrn-AtomicLearning_tool' )
AND ( ( ( course_application.enable_ind ) = 'Y'
OR ( course_application.visible_ind ) = 'N' )
OR ( ( course_application.enable_ind ) = 'N'
OR ( course_application.visible_ind ) = 'Y' ) )
And all of the nested joins are giving me a headache. I expected SQL Server 2008 with the query designer to be able to put in at least aliases, but I guess I was wrong.
When I am in the editor, I am getting an error for users.pk1
in the first join that says
The multi-part identifier "users.pk1" could not be bound
I also get this error:
The objects "course_main" and "course_main" in the FROM clause have the same exposed names. Use correlation names to distinguish them
I know vaguely about aliases, and know how to put them into SQL for very, very basic tasks, but I am just going to confuse myself to the point of no return. I've read all I could about aliases but just don't know where to properly use them in a join this complex. Please help!
Thank you
edit: I'm not really sure how to state what my intent is. Basically I'm trying to get the 3 columns out of all of the tables I'm selecting from. To get the correct data the keys and all that have to match between the tables. In SQL Server 2008 I selected the tables that all have relationships. I've run queries before where all I was selecting from was the course_main and course_application table. Since those have a direct relationship a single join was all that was needed to make that happen. Adding in these 2 extra tables(users and course_users to define which users match up as a specific role in our database against the keys in the other tables)made it a lot more complicated.
These are the schemas for the four tables: