【发布时间】:2019-04-08 15:28:02
【问题描述】:
我有一个这样的 MySQL 表:
##customer##
+-----------+----+---------+
|customer_id|name|telephone|
+-----------+----+---------+
| 1 |Andi|+62932011|
| 2 |Boby|+62928291|
| 3 |Jane|+62932212|
| 4 |John|+62999021|
| 5 |Beth|+62999021|
| 6 |Noel|+62999021|
+-----------+----+---------+
##plus_membership##
+-----------------+-----------+-------+------------+
|plus_membership_id|customer_id|status |requested_at|
+------------------+-----------+-------+------------+
| 1 | 1 | 1 | 2018-11-01 |
| 2 | 2 | 0 | 2018-11-03 |
| 3 | 4 | 2 | 2018-11-04 |
| 4 | 6 | 1 | 2018-11-05 |
+------------------+-----------+-------+------------+
上述结构中有两个表,第一个是customer,主键为customer_id,第二个是plus_membership,外键为customer_id,plus_membership表是表格显示客户请求成为 plus 会员的请求,状态 1 表示客户被批准成为 plus 会员。我需要选择客户表并添加别名列假设别名列名称为成员资格,仅显示regular 或plus,plus 表示plus_membership 状态的客户为1,如果客户不存在于 plus_membership 表中或状态不是 1 在成员资格表中。例如:
SELECT *, .... AS membership FROM customer;
+-----------+----+---------+----------+
|customer_id|name|telephone|membership|
+-----------+----+---------+----------+
| 1 |Andi|+62932011| Plus |
| 2 |Boby|+62928291| Regular |
| 3 |Jane|+62932212| Regular |
| 4 |John|+62999021| Regular |
| 5 |Beth|+62999021| Regular |
| 6 |Noel|+62999021| Plus |
+-----------+----+---------+----------+
【问题讨论】:
-
plus_membership表中的特定customer_id可以有多个条目吗? -
@MadhurBhaiya no only 1