查询无法从php页面工作

时间:2014-03-05 06:52:57

标签: php mysql case mysql-workbench case-when

<?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*4=@sumstatus 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替换,查询如下所示;

 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 workbench中效果很好,当我尝试从php页面执行此查询时,在phpmyadmin中它不起作用,请帮我解决这个案例

1 个答案:

答案 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*4=@sumstatus 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);
    ?>

在我们使用之前需要为当前会话设置临时会话变量。