2016-06-25 94 views
-1

下面的SQL代碼運行在MySQL工作臺得很好,但返回MySQL的語句工作在工作臺上,但錯誤在PHP

「的字符串(228)」您的SQL語法錯誤;檢查對應於您的MariaDB服務器版本的手冊 ,以便在「SELECT p2.this_year,year(p.PAID_DATE)」附近使用正確的語法 以使用current_year,c1.s1,c2.p1,c3.p2,ly.last_ 「在

在PHP 6" 行。我搜索的答案,都無濟於事。不知道是否有人有任何想法。在此先感謝。

$dbinfo = dbinfo(); 
$con = mysqli_connect($dbinfo['localhost'],$dbinfo['localuser'],$dbinfo['localpass'],$dbase); 
if (!$con) die('Could not connect: ' . mysqli_error($con)); 

$sql = " 
SET @startdate = DATE(CURDATE()- INTERVAL DAYOFYEAR(CURDATE())+1 DAY), @enddate = CURDATE(), 
    @laststart = DATE_SUB(@startdate, INTERVAL 1 YEAR), 
    @lastend = DATE_SUB(@enddate, INTERVAL 1 YEAR) 
    ; 

SELECT p2.this_year,year(p.PAID_DATE) as current_year,c1.s1,c2.p1,c3.p2,ly.last_year, 
    lyc1.s1,lyc2.p1,lyc3.p2 
FROM btb.payments as p 

join (
    select CLIENTID,sum(TOTAL) this_year,PAID_DATE 
    from btb.payments 
    where PAID_DATE between @startdate and @enddate 
) as p2 

join (
    select id_no,count(SUMMER_PT1) S1 
    from btb.Client 
    where SUMMER_PT1 <> '' 
) as c1 
    on c1.ID_NO = p.CLIENTID 

join (
    select id_no,count(PART1) P1 
    from btb.Client 
    where PART1 <> '' 
) as c2 

join (
    select id_no,count(PART2) P2 
    from btb.Client 
    where PART2 <> '' 
) as c3 

join (
    SELECT CLIENTID,sum(TOTAL) as last_year 
    FROM btb.payments 
    where PAID_DATE between @laststart and @lastend 
) as ly 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) s1 
FROM btb.Details d 
join btb.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between @laststart and @lastend 
    and d.year = year(@laststart) 
    and d.SESSION = 'S1' 
) as lyc1 
    -- on c1.ID_NO = p.CLIENTID 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) p1 
FROM btb.Details d 
join btb.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between @laststart and @lastend 
    and d.year = year(@laststart) 
    and d.SESSION = 'P1' 
) as lyc2 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) p2 
FROM btb.Details d 
join btb.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between @laststart and @lastend 
    and d.year = year(@laststart) 
    and d.SESSION = 'P2' 
) as lyc3 

where p.PAID_DATE between @startdate and @enddate 
;"; 

$result = mysqli_query($con, $sql); 
if($result === false) { 
    var_dump(mysqli_error($con)); 
} 
else { 
    print_r(mysqli_num_rows($result)); 
} 
+0

這真的和PHP有什麼關係嗎?看起來你的代碼只包含SQL .. – YakovL

+0

對不起,編輯並添加了PHP代碼。 –

+0

您是否允許在一個'mysqli'查詢中使用多個sql語句?我會在同一個查詢中嘗試一個簡單的「集合」和一個簡單的查詢。 –

回答

1

看來,多個SQL在mysqli_query語句不起作用,所以我改變代碼如下,瞧!

$dbase = "btb"; 

$dbinfo = dbinfo(); 
$con = mysqli_connect($dbinfo['localhost'],$dbinfo['localuser'],$dbinfo['localpass'],$dbase); 
if (!$con) die('Could not connect: ' . mysqli_error($con)); 
$startdate = date('Y-m-d', strtotime('01/01')); 
$enddate = date("Y-m-d"); 
$startlast = date("Y-m-d", strtotime($startdate . ' -1 year')); 
$endlast = date("Y-m-d", strtotime($enddate . ' -1 year')); 

echo "$startdate - $enddate = $startlast = $endlast"; 

$sql = " 
SELECT 
    p2.this_year,year(p.PAID_DATE) as current_year,c1.s1,c2.p1,c3.p2,ly.last_year, 
    lyc1.s1 as ly_s1,lyc2.p1 as ly_p1,lyc3.p2 as ly_p2 

FROM $dbase.payments p 

join (
    select CLIENTID,sum(TOTAL) this_year,PAID_DATE 
    from $dbase.payments 
    where PAID_DATE between '$startdate' and '$enddate' 
) as p2 

join (
    select id_no,count(SUMMER_PT1) S1 
    from $dbase.Client 
    where SUMMER_PT1 <> '' 
) as c1 
    on c1.ID_NO = p.CLIENTID 

join (
    select id_no,count(PART1) P1 
    from $dbase.Client 
    where PART1 <> '' 
) as c2 

join (
    select id_no,count(PART2) P2 
    from $dbase.Client 
    where PART2 <> '' 
) as c3 

join (
    SELECT CLIENTID,sum(TOTAL) as last_year 
    FROM $dbase.payments 
    where PAID_DATE between '$startlast' and '$endlast' 
) as ly 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) s1 
FROM $dbase.Details d 
join $dbase.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between '$startlast' and '$endlast' 
    and d.year = year('$startlast') 
    and d.SESSION = 'S1' 
) as lyc1 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) p1 
FROM $dbase.Details d 
join $dbase.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between '$startlast' and '$endlast' 
    and d.year = year('$startlast') 
    and d.SESSION = 'P1' 
) as lyc2 

join (
SELECT d.CLIENTID,d.SESSION,count(p.CLIENTID) p2 
FROM $dbase.Details d 
join $dbase.payments p 
    on p.CLIENTID = d.CLIENTID 
where p.PAID_DATE between '$startlast' and '$endlast' 
    and d.year = year('$startlast') 
    and d.SESSION = 'P2' 
) as lyc3 

where p.PAID_DATE between '$startdate' and '$enddate' 
;"; 

$result = mysqli_query($con, $sql); 
while($row = mysqli_fetch_assoc($result)) { 
    //print_r($row); 
    $this_year = $row['this_year']; 
    $year = $row['current-year']; 
    $this_s1 = $row['s1']; 
    $this_p1 = $row['p1']; 
    $this_p2 = $row['p2']; 
    $last_year = $row['last_year']; 
    $last_s1 = $row['ly_s1']; 
    $last_p1 = $row['ly_p1']; 
    $last_p2 = $row['ly_p2']; 
}