mysql查询需要很长时间才能响应

时间:2018-06-04 16:03:41

标签: mysql

SELECT t.id
     , t.department
     , t.owner
     , t.client
     , u.username as owner_name
     , c.name as catagery
     , d.dept_name as deptname
     , t.periority
     , t.status
     , t.estimate
     , cl.takeaway_name 
  from tbl_task t 
  JOIN tbl_user u 
    ON u.id = t.owner
  JOIN tbl_task_catagery c 
    ON c.id = t.catagery
  JOIN tbl_department d 
    ON d.id = t.department
  JOIN tbl_clients cl 
    ON cl.id = t.client 
   and t.status = 0 
   and (t.id in (select task_id 
                   from tbl_task_note tn 
                  where tn.user_id = '69' 
                    and tn.id in (select max(id) 
                                    from tbl_task_note tt 
                                   where tt.task_id = tn.task_id 
                                 )
                 )
        ) 
  order by t.id

注意:以上查询用于检查用户保留任务。 tbl_task_note 表用于检查单独用户任务的任务说明。

1 个答案:

答案 0 :(得分:0)

With this query you will get the task that have the last task_note registered, including the user, departament, client, and some other. If it is what you need you can just do this.

select 
      t.id,
      t.department,
      t.owner,
      t.client,
      u.username as owner_name,
      c.name as catagery,
      d.dept_name as ptname,
      t.periority,
      t.status,
      t.estimate,
      cl.takeaway_name
      from tbl_task t
      INNER JOIN tbl_user u ON u.id=t.owner
      INNER JOIN tbl_task_catagery c ON c.id=t.catagery
      INNER JOIN tbl_department d ON d.id=t.department
      INNER JOIN tbl_clients cl ON cl.id=t.client and t.status=0 
      INNER JOIN (select * from tbl_task_note where id = 
                       (select max(id) from tbl_task_note)
                 )tb on tb.task_id = t.id
     order by t.id

That way you can improve your query. You shoud also ensure that your keys compared are foreign keys to get faster consults.