Domanda

I have in the database rewards points table :

customer_reward_id  customer_id      order_id        points
1                        1               3             20
2                        1               4             40
3                        1               5             30
4                        1               6             30
5                        1               7             40

Collecting 100 points (sum) and delete from table

where customer_id =1 order by customer_reward_id ASC

any idea?

È stato utile?

Soluzione 2

In real applications, we do not delete the records to deduct the points from the customers. Because every record with the orders are required for reporting and management purposes. Instead the best way to deduct 100 points from the customer is to add the points with a negative sign and then sum up all the points so that you'll get the final value.

For example do the following:

Insert new record with negative value.

insert into reward_points (customer_id,order_id,points) values (1,8,-100);

Then sum the total value as below.

select sum(points) as total_rewards where customer_id=1;

This way you'll keep all the records of the previous orders as well as reduce the rewards too.

Hope this helps for your requirement.

Altri suggerimenti

If it is about deleting record for customer who are having 100 points or more (summed up). Here is the query:

delete from rewards_points 
where customer_id in (
    SELECT customer_id 
    FROM (select * from `rewards_points`) dummy
    group by customer_id
    having sum(points)>=100);

Note: In MySQL you can't specify same table in from clause directly, in which you are performing delete/update operation. So, In this answer I've added extra sub-query and given alias dummy

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top