将带子句查询的sql查询转换为mysql查询

将带子句查询的sql查询转换为mysql查询,mysql,sql,Mysql,Sql,我正在将sql查询转换为mysql查询,但是所有包含WITH子句的查询都失败了 以下是查询: WITH CTE AS ( SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume, ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice,

我正在将sql查询转换为mysql查询,但是所有包含WITH子句的查询都失败了

以下是查询:

WITH CTE AS (
    SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume, 
    ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
    rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
    FROM equitystockoptions_nse AS sto
        JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
    WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
)
SELECT curr.NDate,curr.StkName,curr.StkClosingPrice, curr.StkVolume, curr.IndName, curr.IndClosingPrice, curr.IndVolume,
 LOG(curr.StkClosingPrice / prev.StkClosingPrice) AS StkReturnOnLog, LOG(curr.IndClosingPrice / prev.IndClosingPrice) AS IndReturnOnLog
FROM CTE curr
INNER JOIN CTE PREV ON prev.rownum = curr.rownum - 1 AND curr.StkName = prev.StkName
ORDER BY StkName, NDate;

任何关于此转换的建议都将不胜感激。

让我们为FROM子句中使用的子查询的内联视图指定一个名称。据我所知,MySQL不支持,因此您可以将所有CTE引用替换为实际的子查询:

SELECT  curr.NDate,curr.StkName,curr.StkClosingPrice, curr.StkVolume, 
        curr.IndName, curr.IndClosingPrice, curr.IndVolume,
        LOG(curr.StkClosingPrice / prev.StkClosingPrice) AS StkReturnOnLog, 
        LOG(curr.IndClosingPrice / prev.IndClosingPrice) AS IndReturnOnLog
FROM (
    SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume, 
    ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
    rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
    FROM equitystockoptions_nse AS sto
        JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
    WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
) curr
INNER JOIN (
    SELECT sto.NDate AS NDate,sto.NStockName AS StkName,sto.NClosingPrice AS StkClosingPrice, sto.NVolume AS StkVolume, 
    ind.NStockName AS IndName,ind.NClosingPrice AS IndClosingPrice, ind.NVolume AS IndVolume,
    rownum = ROW_NUMBER() OVER (PARTITION BY sto.NStockName ORDER BY sto.NDate)
    FROM equitystockoptions_nse AS sto
        JOIN equitystockoptions_indices AS ind ON sto.NDate = ind.NDate
    WHERE ind.NStockName='NIFTY' AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) >= DATE_FORMAT(v_fromdate,'%m/%d/%Y') AND STR_TO_DATE(DATE_FORMAT(sto.NDate,'%m/%d/%Y')) <= DATE_FORMAT(v_todate,'%m/%d/%Y')
) PREV ON prev.rownum = curr.rownum - 1 AND curr.StkName = prev.StkName
ORDER BY StkName, NDate;

MySQL也不支持使用rownum=。。。SYNTAX请解释如何在这种情况下查找行号。这不仅是常见的表表达式,查询还使用窗口函数和定义列别名的非标准方式。