-2

以下是返回零行的查询,实际上它不应该:

SELECT distinct(usr.user_id)
FROM
    OCN.users AS usr,
    OCN.users_groups_subscribe AS usr_grp,
    OCN.tests_users AS test_usr
WHERE
    usr.user_id=usr_grp.subscribe_user_id AND
    usr.user_id=test_usr.test_user_user_id AND
    test_user_test_id=116 AND
    (usr.user_first_name LIKE '%ajay%' OR usr.user_last_name LIKE '%ajay%')
ORDER BY usr.user_first_name, usr.user_last_name
4

3 回答 3

4

唯一可能的问题是test_user_test_id。我想说这可能是test_user.test_user_test_id基于您的其他专栏test_user.test_user_user_id

尝试:

SELECT distinct(usr.user_id)
FROM
    OCN.users AS usr,
    OCN.users_groups_subscribe AS usr_grp,
    OCN.tests_users AS test_usr
WHERE
    usr.user_id=usr_grp.subscribe_user_id AND
    usr.user_id=test_usr.test_user_user_id AND
    test_usr.test_user_test_id=116 AND
    (usr.user_first_name LIKE '%ajay%' OR usr.user_last_name LIKE '%ajay%')
ORDER BY usr.user_first_name, usr.user_last_name
于 2013-08-22T06:55:22.010 回答
1

首先,使用正确的 ANSI 连接语法:

SELECT distinct usr.user_id
FROM OCN.users AS usr
    inner join OCN.users_groups_subscribe AS usr_grp on usr_grp.subscribe_user_id = usr.user_id
    inner join OCN.tests_users AS test_usr on test_usr.test_user_user_id = usr.user_id
WHERE
    test_usr.test_user_test_id=116 AND
    (usr.user_first_name LIKE '%ajay%' OR usr.user_last_name LIKE '%ajay%')
ORDER BY usr.user_first_name, usr.user_last_name

现在尝试一一评论您的联接(连同相应的列),看看为什么您没有得到任何行,例如

SELECT distinct usr.user_id
FROM OCN.users AS usr
    --inner join OCN.users_groups_subscribe AS usr_grp on usr_grp.subscribe_user_id = usr.user_id
    inner join OCN.tests_users AS test_usr on test_usr.test_user_user_id = usr.user_id
WHERE
    test_usr.test_user_test_id=116 AND
    (usr.user_first_name LIKE '%ajay%' OR usr.user_last_name LIKE '%ajay%')
ORDER BY usr.user_first_name, usr.user_last_name
于 2013-08-22T07:00:52.550 回答
0

这对我来说很神奇:

SELECT distinct usr.user_id 
FROM OCN.users AS usr
    left join OCN.users_groups_subscribe AS usr_grp on usr_grp.subscribe_user_id = usr.user_id
    left join OCN.tests_users AS test_usr on test_usr.test_user_user_id = usr.user_id
WHERE
    test_usr.test_user_test_id=116 AND
    (usr.user_first_name LIKE '%pramod%' OR usr.user_last_name LIKE '%pramod%')
ORDER BY usr.user_first_name, usr.user_last_name
于 2013-08-22T08:04:07.863 回答