Fetching a result twice with deferend name

Posted by user1799842 on Stack Overflow See other posts from Stack Overflow or by user1799842
Published on 2012-11-05T10:57:44Z Indexed on 2012/11/05 11:00 UTC
Read the original article Hit count: 163

Filed under:
|
|
|
|


I have 1 table with tasks named opentask:
columns: id,title,description,expires,creator_id,creator_name, executer_id, executer_name, priority_id, status_id
1 table with users named user:
with columns: user_id, username
What I want is to create a query where there will be all columns from opentask table where the executer_id will be equal to the user_id of user table AND the creator_id will be equal to the user_id again. This creates a confusion because the first equality excludes the second.

So I need somehow to create a query where I will include the usernames for the executer with something like where "opentask.executer_id=user_user_id" and at the same time I will include the username again (as a differend name?) for the creator with something like "where opentask.executer_id=user_user_id"

So I try this, which of course I know that is missing something, can you help?

SELECT DISTINCT id, title, description, expires, creator_id, executer_id, oc_opentask.priority_id, oc_opentask.status_id, priority_name, status_name, user_id, username, (SELECT username FROM oc_opentask, oc_user WHERE oc_opentask.creator_id=oc_user.user_id) AS username2 FROM oc_opentask, oc_opentask_priority, oc_user, oc_opentask_status WHERE oc_opentask.priority_id=oc_opentask_priority.priority_id AND oc_opentask.executer_id=oc_user.user_id AND oc_opentask.status_id=oc_opentask_status.status_id ORDER BY oc_opentask.expires DESC

© Stack Overflow or respective owner

Related posts about mysql

Related posts about query