[英]I want sql query to change membership status
My site has users with user_id and Customer_id.我的站点有具有 user_id 和 Customer_id 的用户。 Default membership status is pending.
默认成员资格状态为待定。 I want a query to change membership status to active by checking details in the other 2 tables.
我想要一个查询,通过检查其他 2 个表中的详细信息来将成员资格状态更改为活动状态。 If users have bought a membership plan, change their status to active.
如果用户购买了会员计划,将其状态更改为活动。
step 1- I want to select all users with pending status from the "membership" table.第 1 步-我想 select 所有用户从“会员”表中的待处理状态。
table name: membership
+---------+---------+
| user_id | status |
+---------+---------+
| 1 | pending |
| 2 | active |
| 3 | pending |
| 4 | pending |
| 5 | active |
+---------+--------+
step 2- select customer_id of users from step 1.步骤 2- select 来自步骤 1 的用户的 customer_id。
table name: Customers
+---------+--------------+
| user_id | Customer_id |
+---------+--------------+
| 1 | 17 |
| 2 | 18 |
| 3 | 21 |
| 4 | 25 |
| 5 | 29 |
+---------+--------------+
step 3- check if the selected customers from step 2 have ordered product_id 92. if yes then change status from pending to active in the "membership" table.第 3 步 - 检查第 2 步中选择的客户是否订购了 product_id 92。如果是,则在“会员资格”表中将状态从待定更改为活动。
table name: orders
+-------------+------------+
| Customer_id | Product_id |
+-------------+------------+
| 21 | 85 |
| 25 | 92 |
| 29 | 99 |
+-------------+------------+
End Result must look like最终结果必须看起来像
table name: membership
+---------+---------+
| user_id | status |
+---------+---------+
| 1 | pending |
| 2 | active |
| 3 | pending |
| 4 | active |
| 5 | active |
+---------+--------+
You can use update with join syntax:您可以使用 update with join 语法:
update membership m
join Customers c on m.user_id = c.user_id
join orders o on c.Customer_id = o.Customer_id and o.Product_id = 92
set m.status = 'active'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.