2015-05-19 160 views
0

下面是SQL Server查詢我想轉換到MySQL:轉換的SQL Server SQL查詢到MySQL

SELECT SaleDetail.cTaxiNo, Category.cCarName, qryDriverExpenses.nAmount, 
     Sale.nInvoiceNo, Sale.cCustomerName, IIf([cPackageType] ='GENERAL', 
     [nProviderRate],[nProviderRate]/[nProviderKM]) AS nARate, SaleDetail.nKM, 
     SaleDetail.nAmount, Sale.dInvoiceDate, qryFuel.nAmount, 
     SaleDetail.nProviderKM, SaleDetail.nProviderRate 
FROM qryFuel 
RIGHT JOIN (((Sale INNER JOIN SaleDetail ON Sale.nSalePk = SaleDetail.nSalePk) 
INNER JOIN (CarMaster 
INNER JOIN Category 
ON CarMaster.nCategoryPk = Category.nCategoryPK) ON SaleDetail.cTaxiNo = 
    CarMaster.cCarNo) 
LEFT JOIN qryDriverExpenses 
    ON SaleDetail.nTourBookingpk = qryDriverExpenses.nTourBookingPK) 
ON qryFuel.nTourBookingPK = 
    SaleDetail.nTourBookingpk 

ORDER BY Sale.nInvoiceNo, Sale.dInvoiceDate; 

我得到錯誤的SQL

語法錯誤。附近[cPackageType]

請幫助!

+1

有在MYSQL中沒有IIF,而是用IF來代替;如果([cPackageType] ='GENERAL', [nProviderRate],[nProviderRate]/[nProviderKM])AS nARate, – knkarthick24

回答

0

SQL ServerIIfIfMySQL

SELECT SaleDetail.cTaxiNo, Category.cCarName, qryDriverExpenses.nAmount, 
    Sale.nInvoiceNo, Sale.cCustomerName, 
    IF(cPackageType ='GENERAL', nProviderRate, nProviderRate/nProviderKM) AS nARate, 
    SaleDetail.nKM, SaleDetail.nAmount, Sale.dInvoiceDate, qryFuel.nAmount, 
    SaleDetail.nProviderKM, SaleDetail.nProviderRate 
FROM qryFuel RIGHT JOIN 
(((Sale INNER JOIN SaleDetail ON Sale.nSalePk =    
    SaleDetail.nSalePk) INNER JOIN (CarMaster INNER JOIN Category ON 
CarMaster.nCategoryPk = Category.nCategoryPK) ON SaleDetail.cTaxiNo = 
CarMaster.cCarNo) LEFT JOIN qryDriverExpenses ON SaleDetail.nTourBookingpk 
= qryDriverExpenses.nTourBookingPK) 
ON qryFuel.nTourBookingPK = SaleDetail.nTourBookingpk  
ORDER BY Sale.nInvoiceNo, Sale.dInvoiceDate; 
+0

非常感謝您的支持 – Surajghosi

0

使用類別的這些連接是自找麻煩。請研究正確的加入語法,https://technet.microsoft.com/en-us/library/ms191517%28v=sql.105%29.aspx。下面是格式化的查詢:

SELECT SaleDetail.cTaxiNo 
,Category.cCarName 
,qryDriverExpenses.nAmount 
,Sale.nInvoiceNo 
,Sale.cCustomerName 
,CASE WHEN `cPackageType` ='GENERAL' THEN 
     `nProviderRate` 
    ELSE 
     `nProviderRate`/`nProviderKM` 
    END AS nARate 
,SaleDetail.nKM 
,SaleDetail.nAmount 
,Sale.dInvoiceDate 
,qryFuel.nAmount 
,SaleDetail.nProviderKM 
,SaleDetail.nProviderRate 
FROM qryFuel 
INNER JOIN SaleDetail 
    ON qryFuel.nTourBookingPK = SaleDetail.nTourBookingpk 
RIGHT OUTER JOIN Sale 
    ON Sale.nSalePk = SaleDetail.nSalePk 
INNER JOIN CarMaster 
    ON SaleDetail.cTaxiNo = CarMaster.cCarNo 
INNER JOIN Category 
    ON CarMaster.nCategoryPk = Category.nCategoryPK 
LEFT OUTER JOIN qryDriverExpenses 
    ON SaleDetail.nTourBookingpk = qryDriverExpenses.nTourBookingPK 
ORDER BY Sale.nInvoiceNo, Sale.dInvoiceDate; 
+2

實際上TSQL + SQL Server確實使用IIF https://msdn.microsoft.com/ EN-GB /庫/ hh213574.aspx。此外,操作是試圖從SQL Server轉換到MySQL –

+0

啊,我一直忘記他們在2012年添加它。謝謝! – TTeeple

+0

由於方括號,查詢仍然會拋出相同的錯誤。 –

0
SELECT SaleDetail.cTaxiNo, Cate;ry.cCarName, qryDriverExpenses.nAmount, 
    Sale.nInvoiceNo, Sale.cCustomerName, If(`cPackageType` ='GENERAL', 
`nProviderRate`,`nProviderRate`/`nProviderKM`) AS nARate, SaleDetail.nKM, 
SaleDetail.nAmount, Sale.dInvoiceDate, qryFuel.nAmount, 
SaleDetail.nProviderKM, SaleDetail.nProviderRate 


    FROM qryFuel RIGHT JOIN (((Sale INNER JOIN SaleDetail ON Sale.nSalePk =    
    SaleDetail.nSalePk) INNER JOIN (CarMaster INNER JOIN Cate;ry ON 
CarMaster.nCate;ryPk = Cate;ry.nCate;ryPK) ON SaleDetail.cTaxiNo = 
CarMaster.cCarNo) LEFT JOIN qryDriverExpenses ON SaleDetail.nTourBookingpk 
= qryDriverExpenses.nTourBookingPK) ON qryFuel.nTourBookingPK = 
SaleDetail.nTourBookingpk 

    ORDER BY Sale.nInvoiceNo, Sale.dInvoiceDate; 

引用的列名應該由蜱包圍,而不是方括號。

0

試試這個。 MySQL使用IF,而不是IIF並沒有像方括號之一:

SELECT SaleDetail.cTaxiNo, Category.cCarName, qryDriverExpenses.nAmount, 
    Sale.nInvoiceNo, Sale.cCustomerName, If(cPackageType ='GENERAL', 
nProviderRate,nProviderRate/nProviderKM) AS nARate, SaleDetail.nKM, 
SaleDetail.nAmount, Sale.dInvoiceDate, qryFuel.nAmount, 
SaleDetail.nProviderKM, SaleDetail.nProviderRate 


    FROM qryFuel RIGHT JOIN (((Sale INNER JOIN SaleDetail ON Sale.nSalePk =    
    SaleDetail.nSalePk) INNER JOIN (CarMaster INNER JOIN Category ON 
CarMaster.nCategoryPk = Category.nCategoryPK) ON SaleDetail.cTaxiNo = 
CarMaster.cCarNo) LEFT JOIN qryDriverExpenses ON SaleDetail.nTourBookingpk 
= qryDriverExpenses.nTourBookingPK) ON qryFuel.nTourBookingPK = 
SaleDetail.nTourBookingpk 

    ORDER BY Sale.nInvoiceNo, Sale.dInvoiceDate; 
0

IIF是無效的MySQL中

變化

IIf([cPackageType] ='GENERAL', 
     [nProviderRate],[nProviderRate]/[nProviderKM]) AS nARate 

if [cPackageType] = 'GENERAL' then nProviderRate 
else nProviderRate/nProviderKM 
end as nARate 
+0

感謝millen - 進行了更正 – mson