Sql 通过WHERE访问结果

Sql 通过WHERE访问结果,sql,select,where,Sql,Select,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_us

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

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属性的
executer\u username
creator\u username
,以将其与值进行比较,因此我尝试的是弹出mystake:

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' 

比尔确实存在于数据库中,但我访问该值的命令不正确,有人能帮我吗?谢谢

您有一个列别名:

oc_user2.username AS creator_username
在where子句中,您使用的是别名。您需要使用实际的列名,因此将其更改为:

where oc_user2.username = 'bill'

您在where子句中使用了错误的表来获取创建者名称。使用
oc_user2
表来获取用户名(创建者名称)。此外,我只是在想,可能还有一个与
案例
相关的问题,因此建议在比较之前使用
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')

我只需在右侧的用户
下方
,就可以使
任何有效名称
不分大小写。

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

您遇到了什么错误?你的问题令人困惑;我看到您有
oc\u user2.username作为创建者\u username
,但稍后请参阅
oc\u opentask.creator\u username
creator\u username
是否存在于
opentask
表中?这不仅仅是:WHERE oc\u user2.username='bill'是否存在于相同的情况下,即
bill
而不是
bill
?谢谢大家,事实上,我不得不使用WHERE oc\u user2.username='bill'