0

好吧,在我在这里的第一个问题之后,我得到了以下正确且经过测试的代码:

SELECT DISTINCT
id, title, description, expires,
creator_id,executer_id,
oc_opentask.priority_id,
oc_opentask.status_id, priority_name, status_name,
oc_user1.username AS executer_username,
oc_user2.username AS creator_username
FROM oc_opentask
INNER JOIN oc_opentask_priority 
ON oc_opentask.priority_id=oc_opentask_priority.priority_id
INNER JOIN oc_opentask_status 
ON oc_opentask.status_id=oc_opentask_status.status_id
INNER JOIN oc_user AS oc_user1 
ON oc_opentask.executer_id=oc_user1.user_id
INNER JOIN oc_user AS oc_user2 
ON oc_opentask.creator_id=oc_user2.user_id

我的下一个问题是:我想访问它们executer_usernamecreator_username使用 WHERE 属性将其与值进行比较,所以我正在尝试的是:

SELECT DISTINCT
id, title, description, expires,
creator_id,executer_id,
oc_opentask.priority_id,
oc_opentask.status_id, priority_name, status_name,
oc_user1.username AS executer_username,
oc_user2.username AS creator_username
FROM oc_opentask
INNER JOIN oc_opentask_priority 
ON oc_opentask.priority_id=oc_opentask_priority.priority_id
INNER JOIN oc_opentask_status 
ON oc_opentask.status_id=oc_opentask_status.status_id
INNER JOIN oc_user AS oc_user1 
ON oc_opentask.executer_id=oc_user1.user_id
INNER JOIN oc_user AS oc_user2 
ON oc_opentask.creator_id=oc_user2.user_id
WHERE oc_opentask.creator_username='bill' 

bill 确实存在于 db 中,但我访问该值的命令不正确,any1 可以帮助我吗?谢谢

4

3 回答 3

1

您有一个列别名:

oc_user2.username AS creator_username

在你的 where 子句中,你正在使用别名。您需要使用实际的列名,因此将其更改为:

where oc_user2.username = 'bill'
于 2012-11-06T16:15:15.627 回答
0

如果这是 MySQL,只需使用 HAVING 代替 WHERE。然后,无论您是指定原始列名还是您定义的别名,您都不会遇到问题。HAVING 还允许您使用聚合函数的结果,即 HAVING MAX(salary) > 40,而 WHERE 也失败。

于 2014-01-20T02:59:24.587 回答
0

您使用错误的表来获取 where 子句中的创建者名称。使用oc_user2表获取用户名(创建者名称)。另外我只是想仍然可能存在case相关问题,因此建议LOWER在比较之前使用函数,如下所示:

    SELECT DISTINCT
       id, title, description, expires,
       creator_id,executer_id,
       oc_opentask.priority_id,
       oc_opentask.status_id, priority_name, status_name,
       oc_user1.username AS executer_username,
       oc_user2.username AS creator_username
    FROM oc_opentask
    INNER JOIN oc_opentask_priority 
       ON oc_opentask.priority_id=oc_opentask_priority.priority_id
    INNER JOIN oc_opentask_status 
       ON oc_opentask.status_id=oc_opentask_status.status_id
    INNER JOIN oc_user AS oc_user1 
       ON oc_opentask.executer_id=oc_user1.user_id
    INNER JOIN oc_user AS oc_user2 
       ON oc_opentask.creator_id=oc_user2.user_id
    WHERE LOWER(oc_user2.username)=LOWER('bill')

我只是LOWER在右侧使用,使其与大小写无关,any valid name无论大小写如何。

于 2012-11-06T16:15:51.543 回答