0

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:

http://pastebin.com/AJuiGv9s

4

3 回答 3

2

正如其他人所说,您的问题是由于混合连接类型。我认为这对你有用。

您也加入了course_main两次,我认为您可以将其浓缩为course_application桌面上的一次加入。

SELECT 
     u.user_id
    ,cm.course_name
    ,cm.course_id
FROM users AS u
CROSS JOIN course_main AS cm
JOIN course_application AS ca
  ON u.pk1 = ca.pk1
 AND ca.crsmain_pk1 = cm.pk1
JOIN course_u AS cu
  ON u.pk1 = cu.u_pk1
 AND cm.pk1 = cu.crsmain_pk1
 AND cm.pk1 = cu.child_crsmain_pk1
WHERE cu.role = 'P'
  AND cm.course_id LIKE '%FA2013'
  AND ca.application = 'alrn-AtomicLearning_tool' 

  AND (( 
        ca.enable_ind  = 'Y'
     OR ca.visible_ind = 'N' 
    ) OR (
        ca.enable_ind  = 'N'
     OR ca.visible_ind = 'Y' 
    ))

我在想,而不是和之间的交叉连接userscourse_application你真的想要数据库中用户和应用程序的每个组合都有一行吗?),你可能想要引入一个常规INNER JOIN并加入某个键。

于 2013-07-02T19:40:10.620 回答
2

我建议你总是为你的表使用别名,像这样

select
   u.user_id,
   cm.course_name,
   cm.course_id
from users as u
   cross join course_main as cm
   inner join course_application as ca on u.pk1 /*(error #1)*/ = ca.pk1
   inner join course_main as cm2/*(error #2)*/ on ca.crsmain_pk1 = cm.pk1
   inner join course_users as cu on u.pk1 /*(error #3)*/ = cu.users_pk1
....

您也在混合连接样式 - 我建议使用实际连接而不是编写from users, course_main

我敢肯定,当您使用别名重写查询时,会更容易发现错误

于 2013-07-02T19:32:52.430 回答
0

根据给出的所有建议重新编写查询。

use BBLEARN
SELECT u.user_id,
    cm.course_name,
    cm.course_id
FROM users as u
join course_users cu on u.pk1 = cu.users_pk1
join course_main cm on cu.crsmain_pk1=cm.pk1
join course_application ca on cm.pk1 = ca.crsmain_pk1

WHERE cu.role='P'
AND
((cm.course_id) LIKE '%FA2013' AND (ca.application) = 'alrn-AtomicLearning_tool')
AND (((ca.enable_ind) = 'Y' OR (ca.visible_ind) = 'N')
OR ((ca.enable_ind) = 'N' OR (ca.visible_ind) = 'Y'))

http://pastebin.com/vSVVvyqK

于 2013-07-03T13:18:59.843 回答