将SQL Server SQL查询转换为MySQL

时间:2015-05-19 12:51:06

标签: mysql sql sql-server

以下是我想要转换为MySQL的SQL Server查询:

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]

请帮忙!

5 个答案:

答案 0 :(得分:0)

IIf中的<SQL Server If MySQL位于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;

    Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(System.currentTimeMillis());
                        calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
                        calendar.set(Calendar.MINUTE,
                                Integer.parseInt(min_am_pm[0]));
                        // calendar.set(Calendar.AM_PM, am_pm_integer);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);

                        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
                            calendar.add(Calendar.DAY_OF_MONTH, 1);
                        }

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
                            pendingIntent);

答案 1 :(得分: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 :(得分: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; 

引用的列名应该用刻度线括起来,而不是方括号。

答案 3 :(得分: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;

答案 4 :(得分:0)

IIF在MySQL中无效

更改

eps

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