MySQL从Select中删除

时间:2013-12-14 08:56:03

标签: mysql sql

这是我的数据库表tbl_rate

delimiter $$

CREATE TABLE `tbl_rate` (
  `Rate_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Route_ID` int(11) NOT NULL,
  `From_LocationID` int(11) NOT NULL,
  `To_LocationID` int(11) NOT NULL,
  `Normal_Rate` double NOT NULL,
  `Discounted_Rate` double NOT NULL,
  PRIMARY KEY (`Rate_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=289 DEFAULT CHARSET=latin1$$

从tbl_rate执行SQL语句

SELECT Rate_ID FROM tbl_rate WHERE 

From_LocationID NOT IN(
select 
   a.Route_LocationID from_loc_id
from tbl_route a 
inner join tbl_route b on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
inner join tbl_location la on la.Location_ID = a.Route_LocationID
inner join tbl_location lb on lb.Location_ID = b.Route_LocationID
order by a.Route_Seq, b.Route_Seq)

AND To_LocationID NOT IN(
select 
   b.Route_LocationID to_loc_id
from tbl_route a 
inner join tbl_route b on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
inner join tbl_location la on la.Location_ID = a.Route_LocationID
inner join tbl_location lb on lb.Location_ID = b.Route_LocationID
order by a.Route_Seq, b.Route_Seq)
AND Route_ID = 3)

这是我从tbl_rate

获得的sql结果
Rate_ID
--------
253
254

我想从表tbl_rate中删除这两个Rate_ID,如何添加删除SQL语句?

3 个答案:

答案 0 :(得分:1)

    DELETE FROM tbl_rate WHERE 

From_LocationID NOT IN(
select 
   a.Route_LocationID from_loc_id
from tbl_route a 
inner join tbl_route b on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
inner join tbl_location la on la.Location_ID = a.Route_LocationID
inner join tbl_location lb on lb.Location_ID = b.Route_LocationID
order by a.Route_Seq, b.Route_Seq)

AND To_LocationID NOT IN(
select 
   b.Route_LocationID to_loc_id
from tbl_route a 
inner join tbl_route b on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
inner join tbl_location la on la.Location_ID = a.Route_LocationID
inner join tbl_location lb on lb.Location_ID = b.Route_LocationID
order by a.Route_Seq, b.Route_Seq)
AND Route_ID = 3

答案 1 :(得分:1)

如果你想在DELETE中使用同一个表中的子查询,那么你要删除的就是将你的查询包装在另外的外部选择中

DELETE 
  FROM tbl_rate 
 WHERE Rate_ID IN
(
  SELECT Rate_ID
    FROM
  (
    SELECT Rate_ID...  --- Your original query goes here
  ) q
);

这是 SQLFiddle 演示

另一种选择是使用多表DELETE语法

DELETE t
  FROM tbl_rate t JOIN
(
  SELECT Rate_ID... --- Your original query goes here
) q
    ON t.Rate_ID = q.Rate_ID;

这是 SQLFiddle 演示


带有子查询的完整DELETE语句可能看起来像

DELETE 
  FROM tbl_rate 
 WHERE Rate_ID IN
(
  SELECT Rate_ID
    FROM
  (
    SELECT Rate_ID 
      FROM tbl_rate 
     WHERE From_LocationID NOT IN
    (
        select a.Route_LocationID from_loc_id
          from tbl_route a 
               inner join tbl_route b 
                   on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
               inner join tbl_location la 
                   on la.Location_ID = a.Route_LocationID
               inner join tbl_location lb 
                   on lb.Location_ID = b.Route_LocationID
         order by a.Route_Seq, b.Route_Seq
    )
       AND To_LocationID NOT IN
    (
        select b.Route_LocationID to_loc_id
          from tbl_route a 
               inner join tbl_route b 
                   on a.Route_ID = b.Route_ID and a.Route_Seq < b.Route_Seq and a.Route_ID = 3
               inner join tbl_location la 
                   on la.Location_ID = a.Route_LocationID
               inner join tbl_location lb 
                   on lb.Location_ID = b.Route_LocationID
         order by a.Route_Seq, b.Route_Seq
    )
       AND Route_ID = 3
   ) q
);

答案 2 :(得分:0)

尝试添加DISTINCT,因为mySQL在子查询中非常谨慎,例如:

 SELECT DISTINCT Rate_ID FROM tbl_rate WHERE ....

所以,就像是:

DELETE FROM tbl_rate WHERE Rate_ID ( SELECT DISTINCT Rate_ID FROM tbl_rate WHERE .... )

另一种方法是创建子查询的别名,如:

DELETE FROM tbl_rate WHERE Rate_ID ( 
      SELECT * FROM (SELECT Rate_ID FROM tbl_rate WHERE .... ) A 
)