2014-03-05 103 views
0
<?php 

$selectdelegatedtasks = "select delegation_cycle.delegation_cycle_id as task_id,delegation_cycle.delegate_from_id as from_id,delegation_cycle.delegate_to_id as to_id,delegation_cycle.time_estimate as main_time_estimate,delegation_cycle.status as main_task_status ,delegation_cycle.start_date as main_start_date,delegation_cycle.changes,DATE(delegation_cycle.delegated_time) as orginal_email_time,tasks.task_name,tasks.client_id,tasks.followup,tasks.task_owner,tasks.main_due_date,tasks.main_priority,1 as delegated,delegation_user_relation.showinlist,users.email as email_from,clients.client_name, @delegationcount:=(select count(delegation_cycle_id) from delegation_cycle where delegation_cycle.task_id=tasks.task_id and delegation_cycle.task_parent='$loggeduserid') as delegationcount, @sumstatus:=(select sum(status) from delegation_cycle where delegation_cycle.task_id=tasks.task_id and delegation_cycle.task_parent='$loggeduserid' and delegation_cycle.status='4') as sumstatus, (select count(notes_records_id) from notes_records join notes ON notes.notes_id = notes_records.notes_id where notes_to = '$loggeduserid' and notes.task_id = delegation_cycle.task_id) as notescount,(select count(notes_records_id) from notes_records join notes ON notes.notes_id = notes_records.notes_id where notes_to = '$loggeduserid' and notes.task_id = delegation_cycle.task_id and notes_records.read=0) as unreadnotescount from delegation_cycle inner join tasks on tasks.task_id=delegation_cycle.task_id inner join users on users.user_id=delegation_cycle.delegate_from_id inner join clients on clients.client_id=tasks.client_id join delegation_user_relation on delegation_user_relation.delegate_task_id=tasks.task_id where (case when delegation_cycle.status=3 || @delegationcount*[email protected] then delegation_user_relation.user_id='$loggeduserid' and delegation_cycle.delegate_to_id='$loggeduserid' and delegation_cycle.status!=4 else (delegation_user_relation.showinlist=1 || delegation_user_relation.showinlist=2) and delegation_user_relation.user_id='$loggeduserid' and delegation_cycle.delegate_to_id='$loggeduserid' and delegation_cycle.status!=4 end)"; 


     $resultqw1 = $this -> objDBConn -> exceuteQuery($selectdelegatedtasks); 
     print_r($resultqw1); 

?> 

$loggeduserid被4代替,查詢如下所示;查詢不能從php頁面工作

select 
    delegation_cycle.delegation_cycle_id as task_id, 
    delegation_cycle.delegate_from_id as from_id, 
    delegation_cycle.delegate_to_id as to_id, 
    delegation_cycle.time_estimate as main_time_estimate, 
    delegation_cycle.status as main_task_status, 
    delegation_cycle.start_date as main_start_date, 
    delegation_cycle.changes, 
    DATE(delegation_cycle.delegated_time) as orginal_email_time, 
    tasks.task_name, 
    tasks.client_id, 
    tasks.followup, 
    tasks.task_owner, 
    tasks.main_due_date, 
    tasks.main_priority, 
    1 as delegated, 
    delegation_user_relation.showinlist, 
    users.email as email_from, 
    clients.client_name, 
    @delegationcount:=(select 
      count(delegation_cycle_id) 
     from 
      delegation_cycle 
     where 
      delegation_cycle.task_id = tasks.task_id 
       and delegation_cycle.task_parent = '4') as delegationcount, 
    @sumstatus:=(select 
      sum(status) 
     from 
      delegation_cycle 
     where 
      delegation_cycle.task_id = tasks.task_id 
       and delegation_cycle.task_parent = '4' 
       and delegation_cycle.status = '4') as sumstatus, 
    (select 
      count(notes_records_id) 
     from 
      notes_records 
       join 
      notes ON notes.notes_id = notes_records.notes_id 
     where 
      notes_to = '4' 
       and notes.task_id = delegation_cycle.task_id) as notescount, 
    (select 
      count(notes_records_id) 
     from 
      notes_records 
       join 
      notes ON notes.notes_id = notes_records.notes_id 
     where 
      notes_to = '4' 
       and notes.task_id = delegation_cycle.task_id 
       and notes_records.read = 0) as unreadnotescount 
from 
    delegation_cycle 
     inner join 
    tasks ON tasks.task_id = delegation_cycle.task_id 
     inner join 
    users ON users.user_id = delegation_cycle.delegate_from_id 
     inner join 
    clients ON clients.client_id = tasks.client_id 
     join 
    delegation_user_relation ON delegation_user_relation.delegate_task_id = tasks.task_id 
where 
    (case 
     when 
      delegation_cycle.status = 3 
       || @delegationcount * 4 = @sumstatus 
     then 
      delegation_user_relation.user_id = '4' 
       and delegation_cycle.delegate_to_id = '4' 
       and delegation_cycle.status != 4 
     else (delegation_user_relation.showinlist = 1 
      || delegation_user_relation.showinlist = 2) 
      and delegation_user_relation.user_id = '4' 
      and delegation_cycle.delegate_to_id = '4' 
      and delegation_cycle.status != 4 
    end) 

正在使用此查詢,這在MySQL工作臺,在那裏,當我嘗試從PHP頁面,並在phpMyAdmin它不工作執行這個查詢,請幫我解決這個情況下運作良好

+0

將此查詢放入存儲過程並傳遞參數... –

+0

請更新PHP代碼 – Mikpa

+0

的代碼,請檢查 –

回答

0
<?php $sql=" SET @delegationcount:=0;"; 
    $this->objDBConn->exceuteQuery($sql); 
    $sql1=" SET @sumstatus:=0;"; 
    $this->objDBConn->exceuteQuery($sql1); 
    $selectdelegatedtasks = "select delegation_cycle.delegation_cycle_id as task_id,delegation_cycle.delegate_from_id as from_id,delegation_cycle.delegate_to_id as to_id,delegation_cycle.time_estimate as main_time_estimate,delegation_cycle.status as main_task_status ,delegation_cycle.start_date as main_start_date,delegation_cycle.changes,DATE(delegation_cycle.delegated_time) as orginal_email_time,tasks.task_name,tasks.client_id,tasks.followup,tasks.task_owner,tasks.main_due_date,tasks.main_priority,1 as delegated,delegation_user_relation.showinlist,users.email as email_from,clients.client_name, @delegationcount:=(select count(delegation_cycle_id) from delegation_cycle where delegation_cycle.task_id=tasks.task_id and delegation_cycle.task_parent='$loggeduserid') as delegationcount, @sumstatus:=(select sum(status) from delegation_cycle where delegation_cycle.task_id=tasks.task_id and delegation_cycle.task_parent='$loggeduserid' and delegation_cycle.status='4') as sumstatus, (select count(notes_records_id) from notes_records join notes ON notes.notes_id = notes_records.notes_id where notes_to = '$loggeduserid' and notes.task_id = delegation_cycle.task_id) as notescount,(select count(notes_records_id) from notes_records join notes ON notes.notes_id = notes_records.notes_id where notes_to = '$loggeduserid' and notes.task_id = delegation_cycle.task_id and notes_records.read=0) as unreadnotescount from delegation_cycle inner join tasks on tasks.task_id=delegation_cycle.task_id inner join users on users.user_id=delegation_cycle.delegate_from_id inner join clients on clients.client_id=tasks.client_id join delegation_user_relation on delegation_user_relation.delegate_task_id=tasks.task_id where (case when delegation_cycle.status=3 || @delegationcount*[email protected] then delegation_user_relation.user_id='$loggeduserid' and delegation_cycle.delegate_to_id='$loggeduserid' and delegation_cycle.status!=4 else (delegation_user_relation.showinlist=1 || delegation_user_relation.showinlist=2) and delegation_user_relation.user_id='$loggeduserid' and delegation_cycle.delegate_to_id='$loggeduserid' and delegation_cycle.status!=4 end)"; 


    $resultqw1 = $this -> objDBConn -> exceuteQuery($selectdelegatedtasks); 
    print_r($resultqw1); 
    ?> 

在我們使用它之前,需要爲當前會話設置臨時會話變量。