【问题标题】:Make an alias column with custom string in MySQL在 MySQL 中使用自定义字符串创建别名列
【发布时间】: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_idplus_membership表是表格显示客户请求成为 plus 会员的请求,状态 1 表示客户被批准成为 plus 会员。我需要选择客户表并添加别名列假设别名列名称为成员资格,仅显示regularplusplus 表示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

标签: mysql join select alias


【解决方案1】:

您可以在两个表之间使用Left Join,并使用Case .. When 条件表达式来相应地计算membership

Left Join 将确保考虑customer 表中的所有客户,无论他们在plus_membership 表中是否有相应的匹配行。

SELECT
 c.customer_id, 
 c.name, 
 c.telephone, 
 (CASE WHEN pm.status = 1 THEN 'Plus' ELSE 'Regular' END) AS membership
FROM customer AS c
LEFT JOIN plus_membership AS pm 
  ON pm.customer_id = c.customer_id 

另一种方法是使用Correlated SubqueryExists()。一般来说,这种比Left Join方法效率低。

SELECT 
  c.customer_id, 
  c.name, 
  c.telephone, 
  CASE WHEN EXISTS (SELECT 1 
                    FROM plus_membership AS pm 
                    WHERE pm.customer_id = c.customer_id AND 
                          pm.status = 1
                   )
       THEN 'Plus' 
       ELSE 'Regular' 
  END AS membership 
FROM customer AS c

【讨论】:

    【解决方案2】:

    我们使用EXISTSIN 在另一个表中查找数据。

    select customer_id, name, telephone,
      case when customer_id in (select customer_id from plus_membership where status = 1)
           then 'Plus' else 'Regular' end as membership
    from customer
    order by customer_id;
    

    【讨论】:

    • @Madhur Bhaiya:只要没有性能问题,何必担心?这种任务最好使用EXISTSIN 来解决。具有外部连接的解决方案是可能的,但混淆了我们只是选择客户加一个标志。它只在这里有效,因为 OP 告诉你在另一个表中每个客户只能有一行。 (这让我想知道这是否可以保证永远存在,以及为什么有两张桌子。)INEXISTS 完全相同。因此,如果它提出不同的执行计划,它将是一个糟糕的优化器。也许就是这么穷。
    • 糟糕,我要回复的评论到哪里去了? :-)
    • 我删除了评论,因为我在答案中添加了相关子查询解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2019-07-13
    相关资源
    最近更新 更多