2017-10-10 20 views
-2

我有一个SELECT查询:Mysql的转换SELECT查询,删除查询

SELECT Data.VendorProductRateID  
FROM 
(SELECT MIN(VendorProductRateID) VendorProductRateID 
FROM TMP_VendorProductRate 
WHERE VendorProductID = 4 
    AND VendorProductRateTxnInfoID = 89 
GROUP BY DialCode,StartTime) Data, 
TMP_VendorProductRate TMP_VendorProductRate 
WHERE (TMP_VendorProductRate.VendorProductRateID = Data.VendorProductRateID); 

结果:

VendorProductRateID 
123 
124 
125 

我的表有50条记录所以现在我想删除所有记录不大于3的ID。

我有试过下面的查询,但没有得到我的回答:

DELETE FROM `TMP_VendorProductRate` AS CHC WHERE CHC.VendorProductRateID NOT IN 
(
    SELECT Data.VendorProductRateID 
    FROM 
     (SELECT MIN(VPR.VendorProductRateID) VendorProductRateID 
      FROM TMP_VendorProductRate VPR 
      WHERE VPR.VendorProductID = 4 
       AND VPR.VendorProductRateTxnInfoID = 90 
      GROUP BY VPR.DialCode,VPR.StartTime 
     ) 
    Data, 
    TMP_VendorProductRate TMP_VendorProductRate 
    WHERE (TMP_VendorProductRate.VendorProductRateID = Data.VendorProductRateID) 
) 

错误:#1064 - 你在你的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的“AS CHC WHERE CHC.VendorProductRateID NOT IN ( SELECT Data.VendorProductRa”在行1

请帮我正确的语法手册。

+0

任何错误到来或什么? – KMS

+0

DELETE查询中有错误。 –

+0

也分享那个错误.. – KMS

回答

0

喜看到这里删除查询语法,

https://www.w3schools.com/sql/sql_delete.asp

别名不会来了DELETE查询,更改如下图所示,

DELETE FROM `TMP_VendorProductRate` WHERE... 

可能,这将有助于

UPDATE

没有别名,你可以给,

DELETE FROM `TMP_VendorProductRate` WHERE VendorProductRateID NOT IN 
(
    SELECT Data.VendorProductRateID 
    FROM 
     (SELECT MIN(VPR.VendorProductRateID) VendorProductRateID 
      FROM TMP_VendorProductRate VPR 
      WHERE VPR.VendorProductID = 4 
       AND VPR.VendorProductRateTxnInfoID = 90 
      GROUP BY VPR.DialCode,VPR.StartTime 
     ) 
    Data 

    WHERE (TMP_VendorProductRate.VendorProductRateID = Data.VendorProductRateID) 
) 

尝试......

也请告诉我TMP_VendorProductRate TMP_VendorProductRate这意味着什么?

+0

但我需要删除基于NOT IN条件意味着使用子查询,也与相同的表。 –

+0

这就是没有pbm,你可以尝试没有别名的@ SanjayChaudhari – KMS

+0

它会显示错误:#1093 - 你不能指定目标表'TMP_VendorProductRate'在FROM子句更新 –