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

この表を更新したいと思います。

加入者がパッケージのみにサインアップしている場合、loveそれを に変更したいheart.

サブスクライバーがパッケージlove&を持っている場合heartloveの行を削除して、 の行のままにしたいと思いheartます。

これを使ってみた

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

`

両方のパッケージに参加しているサブスクライバーがいるため、少し機能しません

4

1 に答える 1

0

仕様に適切な select ステートメントができたら、大変な作業のほとんどはすでに完了しています。全体を where 句として使用したり、派生テーブルとして結合したりしても、そこから DML ステートメントを作成するのは通常簡単です。

-- 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 に答える