-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));
}
這真的和PHP有什麼關係嗎?看起來你的代碼只包含SQL .. – YakovL
對不起,編輯並添加了PHP代碼。 –
您是否允許在一個'mysqli'查詢中使用多個sql語句?我會在同一個查詢中嘗試一個簡單的「集合」和一個簡單的查詢。 –