mssql_query():message:''(严重级15)

时间:2015-05-27 07:15:32

标签: php sql sql-server left-join

当我运行我的代码时,我收到此错误:

  

警告:mssql_query():message:&bbspplanning'附近的语法错误。   (严重程度15)在第91行/var/www/bstplanning/report/scorecard2.php

     

警告:mssql_query():常规SQL Server错误:检查来自SQL Server的消息(严重级15)   第91行/var/www/bstplanning/report/scorecard2.php

我的代码如下所示:

    if ($team != 'All') {
    $sql_team = "users.split = '" . $team . "' AND";
    $sql_user = "users.user_id LIKE '%'";
} else {
    $sql_team = "users.user_id LIKE '%' AND";
    if ($user != 'All') {
        $sql_user = "users.user_id = '" . $user . "'";
    } else {
        $sql_user = "users.user_id LIKE '%'";
    }
}

$sql = "
SELECT 
        Drive.Owner 
        ,ROUND(AVG(CAST(actual AS FLOAT)), 2) as rezult
        ,ROUND(Sum(CAST(Ontime AS FLOAT))/Sum(task_count), 2) as rezult2
       ,ROUND(sum(case when firsta != 'none' then 1 else 0 end)/ROUND(CAST(count(firsta)AS FLOAT),2)*100,2) nones1
       ,ROUND(sum(case when seconda != 'none' then 1 else 0 end)/ROUND(CAST(count(seconda)AS FLOAT),2)*100,2) nones1
       ,ROUND(CAST(count(dock )AS FLOAT),2)/CAST(sum(case when dock = 'yes' then 1 else 0 end)AS FLOAT)*100 as documentavail
       ,(sum(case when dock = 'yes' then 1 else 0 end)+sum(case when dock = 'old' then 1 else 0 end))/count(dock)*100 as documentavail
FROM (
SELECT 
    users.user_name+ ' ' +users.user_surname AS Owner
    ,users.split as test
    ,sc.firstbackup as firsta
    ,sc.secondbackup as seconda
    ,sc.documentation as dock
    ,sc.active as active
    ,(SELECT CASE WHEN non_sc = '1' THEN 'regular' ELSE 'sc' END) as sc_or_non_sc
    FROM
    sc

INNER JOIN
    users
ON sc.user_id = users.user_id

WHERE
     " . $sql_team . "  " . $sql_user . "
) Drive
left join
(
SELECT
     bstplanning.dbo.sc_data.track
    ,bstplanning.dbo.users.split
    ,bstplanning.dbo.sc_data.country
    ,bstplanning.dbo.sc_data.client
    ,bstplanning.dbo.sc_data.task_group
    ,MAX(ISNULL(bstplanning.dbo.users.user_name,'') + ' ' + ISNULL(bstplanning.dbo.users.user_surname,'')) as Owner
    ,AVG(CASE WHEN bstplanning.dbo.sc_data.ontime = 'on time' THEN 100 ELSE 0 END)*COUNT(bstplanning.dbo.sc_data.country) AS Ontime
    ,AVG(CASE WHEN bstplanning.dbo.sc_data.accuracy = 'accurate' THEN 100 ELSE 0 END) AS actual
    ,COUNT(bstplanning.dbo.sc_data.country) AS task_count

FROM 
    bstplanning.dbo.sc_data 

INNER JOIN bstplanning.dbo.users 
    ON bstplanning.dbo.sc_data.user_id = bstplanning.dbo.users.user_id  

WHERE
    bstplanning.dbo.sc_data.date >= '" . $nuo . "' AND bstplanning.dbo.sc_data.date <= '" . $iki . "' AND  " . $sql_team . " " . $sql_user . " bstplanning.dbo.sc_data.actual > 0 AND
    ((bstplanning.dbo.sc_data.ontime='on time' OR bstplanning.dbo.sc_data.ontime = 'late') OR (bstplanning.dbo.sc_data.accuracy='accurate' OR bstplanning.dbo.sc_data.accuracy = 'error'))

GROUP BY
     bstplanning.dbo.sc_data.country
    ,bstplanning.dbo.sc_data.client
    ,bstplanning.dbo.sc_data.task_group
    ,bstplanning.dbo.users.split
    ,bstplanning.dbo.sc_data.track
    ) Drive2
    on Drive.Owner=Drive2.Owner
    where sc_or_non_sc ='sc'
    group by
        Drive.Owner
";

我不知道什么是错的,因为当我在SQL Server Management中运行代码时,它运行正常。我认为问题在于$ team或$ user,但我不知道如何修复它。这很疯狂,当我删除&#34; Where&#34;从选择它的工作。

1 个答案:

答案 0 :(得分:0)

我发现了什么问题。我不确定为什么,但是当我删除$ team时,$ user从左边加入条件有效。