-1
+-------------------------------+
| id  subscriber       package  |
+-------------------------------+
| 1   5553288          heart    |
| 2   4443365          love     |
| 3   5553288          love     |
| 4   3332353          love     |
| 5   5845569          tech     |
+-------------------------------+

我想更新这张表。

如果订阅者仅注册了套餐love,我想将其更改为heart.

如果订阅者有包love& heart。我想删除 for 的行love并将它们保留在 for 的行中heart

我试过用这个

UPDATE subscriber
SET package=`heart
WHERE package=`love`

`

有点它不会起作用,因为两个包中都有订户

4

1 回答 1

0

一旦你为你的规范有了一个好的选择语句,你就已经完成了大部分艰苦的工作。从中创建 DML 语句通常是微不足道的,即使您使用整个事物作为您的 where 子句或将其作为派生表加入。

-- If a subscriber has signed up for package love only, I'd like to change it to heart.
update MyTable
set package = 'heart'
where subscriber in (
        -- Get subscribers with only love
        select subscriber
        from MyTable
        group by subscriber
        -- This having clause will handle duplicate subscriptions to love
        having max(package) = min(package)
            and max(package) = 'love'
    );

-- And if a subscriber has package love & heart. I'd like to delete the row for love and leave them with the row for heart.
delete from MyTable
where package = 'love'
    and subscriber in (
        -- Get subscribers with heart
        select subscriber
        from MyTable
        where package = 'heart'
    )
    and subscriber in (
        -- Get subscribers with love
        select subscriber
        from MyTable
        where package = 'love'
    );
于 2013-03-18T16:46:14.823 回答