0


我有 1 个表,其中包含名为 opentask 的任务:
列:id、title、description、expires、creator_id、creator_name、executer_id、executer_name、priority_id、status_id
1 个表,用户名为 user:
列:user_id、username
我想要的是创建一个查询 opentask 表中的所有列,其中 executer_id 将等于 user 表的 user_id 并且 creator_id 将再次等于 user_id。这造成了混乱,因为第一个相等排除了第二个。

因此,我需要以某种方式创建一个查询,其中我将包含执行者的用户名,其中包含“opentask.executer_id=user_user_id”之类的内容,同时我将再次包含创建者的用户名(作为不同的名称?)像“opentask.executer_id=user_user_id”这样的东西

所以我试试这个,当然我知道它缺少一些东西,你能帮忙吗?

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

4

1 回答 1

0

使用不同的别名重新JOINoc_user两次,creators如下executers所示:

SELECT DISTINCT 
  t.*, 
  creators.user_name 'Creator name',
  executers.username 'Executor name', 
  tp.priority_name, 
  s.status_name
FROM oc_opentask t 
INNER JOIN oc_opentask_priority tp        ON t.priority_id = tp.priority_id
INNER JOIN oc_user              executers ON t.executer_id = executes.user_id
INNER JOIN oc_user              creators  ON t.creator_id  = creators.user_id
INNER JOIN oc_opentask_status   s         ON t.status_id   = s.status_id
ORDER BY t.expires DESC
于 2012-11-05T11:07:07.940 回答