$sqlgdt="SELECT d.PalletCode, o.CusCode,
d.DocNo, d.SerialCode , d.LogDate , o.DeliveryDate,
case when o.CusCode like '%@Loading%' then tmp_replace.field_replace else o.CusCode end as new_cuscode
FROM dbo.t_GiOrder as o
INNER JOIN dbo.t_GiDetail as d ON o.DocNo = d.DocNo
left join [ตารางที่ต้องการเอามาแทน] as tmp_replace on ........
WHERE( SerialCode LIKE '%".addslashes($_POST["txtKeyword"])."%')
ORDER BY SerialCode";
SELECT dbo.t_GiDetail.PalletCode,
CASE WHEN dbo.t_GiOrder.CusCode = '@Loading' THEN 'AD' ELSE dbo.t_GiOrder.CusCode END CusCode,
dbo.t_GiDetail.DocNo, dbo.t_GiDetail.SerialCode ,
dbo.t_GiDetail.LogDate , dbo.t_GiOrder.DeliveryDate
FROM dbo.t_GiOrder
INNER JOIN dbo.t_GiDetail ON dbo.t_GiOrder.DocNo = dbo.t_GiDetail.DocNo
WHERE ( SerialCode LIKE '%".$_POST["txtKeyword"]."%') ORDER BY SerialCode
<?php
if($_POST["txtKeyword"] != ""){
$sqlgdt="SELECT d.PalletCode, o.CusCode,
d.DocNo, d.SerialCode , d.LogDate , o.DeliveryDate,
case when o.CusCode like '%@Loading%' then tmp_replace.field_replace else o.CusCode end as new_cuscode
FROM dbo.t_GiOrder as o
INNER JOIN dbo.t_GiDetail as d ON o.DocNo = d.DocNo
left join [dbo.t_GiDetail.SerialCode, dbo.t_GiDetail.PalletCode ,
dbo.t_GiOrder.CusCode, dbo.t_GiOrder.DocNo, KKC_Loading.dbo.tran_JobDo.CustomerName
FROM dbo.t_GiDetail INNER JOIN
dbo.t_GiOrder ON dbo.t_GiDetail.DocNo = dbo.t_GiOrder.DocNo INNER JOIN
KKC_Loading.dbo.tran_Job ON dbo.t_GiOrder.DocNo = KKC_Loading.dbo.tran_Job.JobNo INNER JOIN
KKC_Loading.dbo.tran_JobDo ON KKC_Loading.dbo.tran_Job.ID = KKC_Loading.dbo.tran_JobDo.JobID] as tmp_replace on
WHERE( SerialCode LIKE '%".addslashes($_POST["txtKeyword"])."%')
ORDER BY SerialCode";
$querygdt=odbc_exec($link,$sqlgdt);
while($rowgdt=odbc_fetch_array($querygdt)){
$Doc = $rowgdt['DocNo'];
$PalletCode = $rowgdt['PalletCode'];
$SerialCode = $rowgdt['SerialCode'];
$LogDate = $rowgdt['Logdate'];
$CusCode = $rowgdt['CusCode'];
$DeliveryDate = $rowgdt['DeliveryDate'];
$num5 = count ($SerialCode);
?>
<?php
$sqlgdt="
SELECT d.PalletCode, o.CusCode, d.DocNo, d.SerialCode , d.LogDate , o.DeliveryDate
, case
when o.CusCode like '%@Loading%'
then tmp_replace.field_replace #### ตรงนี้เป็น field_replace คือค่าไหนใน sub query ( tmp_replace )
else o.CusCode
end as new_cuscode
FROM dbo.t_GiOrder as o
INNER JOIN dbo.t_GiDetail as d ON o.DocNo = d.DocNo
left join ( #### ส่วนนี้คือ sub query ฟีลด์ไหนไม่ได้ใช้ ก็ไม่ต้องใส่ใน select clause ก็ได้
select dbo.t_GiDetail.SerialCode, dbo.t_GiDetail.PalletCode
, dbo.t_GiOrder.CusCode, dbo.t_GiOrder.DocNo
, KKC_Loading.dbo.tran_JobDo.CustomerName
FROM dbo.t_GiDetail
INNER JOIN dbo.t_GiOrder ON dbo.t_GiDetail.DocNo = dbo.t_GiOrder.DocNo
INNER JOIN KKC_Loading.dbo.tran_Job ON dbo.t_GiOrder.DocNo = KKC_Loading.dbo.tran_Job.JobNo
INNER JOIN KKC_Loading.dbo.tran_JobDo ON KKC_Loading.dbo.tran_Job.ID = KKC_Loading.dbo.tran_JobDo.JobID
) as tmp_replace on ................ #### ส่วนนี่จะอ้างอิง กับ main query กำหนดไว้ด้วย
WHERE SerialCode LIKE '%".addslashes($_POST["txtKeyword"])."%'
ORDER BY SerialCode";
หัดใช้ alias name บ้างจะทำให้โค๊ดดูสอาด และเข้าใจมากขึ้น
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'select'., SQL state 37000 in SQLExecDirect in C:\xampp\htdocs\kulthorn\hp\Check-Stock\stock3.php on line 569
Warning: odbc_fetch_array(): supplied argument is not a valid ODBC result resource in C:\xampp\htdocs\kulthorn\hp\Check-Stock\stock3.php on line 570
แบบนี้ค่ะ
$sqlgdt="
SELECT d.PalletCode, o.CusCode, d.DocNo, d.SerialCode , d.LogDate , o.DeliveryDate
, case
when o.CusCode like '%@Loading%'
then kj.CustomerName
else o.CusCode
end as new_cuscode
FROM dbo.t_GiOrder as o
inner join dbo.t_GiDetail as d ON o.DocNo = d.DocNo
left join (
select j.JobNo, jd.CustomerName
from KKC_Loading.dbo.tran_Job j , KKC_Loading.dbo.tran_JobDo jd
where j.ID = jd.JobID
) kj ON o.DocNo = kj.JobNo
WHERE d.SerialCode LIKE '%".addslashes($_POST["txtKeyword"])."%'
ORDER BY d.SerialCode";