1

我有两个相互关联的表,我只想从一个表中删除记录。表结构如下

Table : Users
userunique, active, unSubscribed

Table : userBilling
userunique, billCreatedDate,billstatus

我想删除那些不再活跃的用户账单,条件如下。

billcreateddate < 2011-12-30 
billstatus=2 //pending

active='0'
unSubscribed<'2011-12-30'

我已经写了一个查询,但这不是我所期望的。我只想从 userBilling 表而不是用户表中删除记录

select * from userBilling ubill, Users user
WHERE ubilll.userUnique=user.userUnique 
AND ubill.billCreatedDate<'2011-12-30'
AND ubill.billstatus = '2'  
AND ((user.active = '0') AND (user.unsubscribed>'2011-01-01') 
AND user.unSubscribed<'2011-12-30')) 

但我猜上面的查询包括两个表的结果。请建议!

谢谢,

4

3 回答 3

3
DELETE  a
FROM    userBilling a
        INNER JOIN users b
            ON a.auserUnique = b.userUnique
WHERE   billcreateddate < 2011-12-30 OR
        billstatus = 2 OR
        active = 0 OR
        unSubscribed < '2011-12-30'

如果您希望所有条件都为,请更改ORAND.

于 2012-12-18T15:05:51.843 回答
1

这应该有效。您应该始终使您的联接明确。这样你就不会感到惊讶了。

DELETE ubill
FROM userBilling ubill
  JOIN Users u
  ON (ubill.userUnique=u.userUnique)
WHERE ubill.billCreatedDate<'2011-12-30'
  AND ubill.billstatus = '2'  
  AND ((u.active = '0') AND (u.unsubscribed>'2011-01-01') 
  AND u.unSubscribed<'2011-12-30')) 
于 2012-12-18T15:06:14.667 回答
0

尝试这样做

     DELETE ubill
      FROM userBilling ubill
     INNER JOIN Users
     ON  ubill.userUnique=user.userUnique
      WHERE  
        ubill.billCreatedDate<'2011-12-30'
        AND ubill.billstatus = '2'  
        AND user.active = '0' 
        AND user.unsubscribed>'2011-01-01' 
        AND user.unSubscribed<'2011-12-30' 
于 2012-12-18T15:09:00.417 回答