【问题标题】:Mysql prefix a column name in Select statementMysql 在 Select 语句中为列名添加前缀
【发布时间】:2016-11-30 11:59:35
【问题描述】:

我可能没有正确理解我的条款,因此无法在网上找到答案。

我有两张这样的桌子

customer_db - Customer Data
CustomerID    CustomerName     CustomerContact
1             John             1234
2             Anna             5678
3             Angel            2468

customer_channel - Chat Channel Name
ChannelName            OnlineStatus
private-customer-1     YES
private-customer-2     NO

所以通常我会做这样的事情

Select a.OnlineStatus, b.CustomerName, b.CustomerContact FROM customer_db a,
customer_channel b WHERE a.CustomerID = b.ChannelName

“private-customer-”前缀在哪里,所以我无法匹配。

我尝试添加前缀,但它不起作用。 如何在 WHERE 语句中选择在列名中添加“文本前缀”的表?

Select a.OnlineStatus, b.CustomerName, b.CustomerContact FROM customer_db a,
customer_channel b WHERE a.CustomerID = 'private-customer-'+b.ChannelName

【问题讨论】:

    标签: mysql


    【解决方案1】:

    使用concat函数,逗号(,)是老办法,尝试使用join:

    SELECT
        a.OnlineStatus,
        b.CustomerName,
        b.CustomerContact
    FROM customer_db a
    JOIN customer_channel b
    ON a.CustomerID = concat('private-customer-', b.ChannelName)
    

    【讨论】:

      【解决方案2】:

      在 WHERE 语句中为列名添加“文本前缀”

      不,除非您将其设为动态查询(准备好的语句),否则您不能这样做。只有列名或表名别名才允许使用前缀或后缀。

      (或)您可以使用子查询并使用该子查询结果执行JOIN,例如

      Select a.OnlineStatus, 
      b.CustomerName,
       b.CustomerContact 
      FROM customer_db a JOIN ( select CustomerName, CustomerContact,
      'private-customer-'+ ChannelName as channelName
      from customer_channel ) b 
      ON a.CustomerID = b.channelName;
      

      【讨论】:

        【解决方案3】:

        试试这个使用 Concat 函数..

          Select a.OnlineStatus,
                 b.CustomerName, 
                 b.CustomerContact 
          FROM customer_db a,
               customer_channel b 
          WHERE a.CustomerID = concat('private-customer-', b.ChannelName)
        

        【讨论】:

          【解决方案4】:

          您可以使用替换或子字符串来执行此操作 例如给定

          drop table if exists a;
          create table a(CustomerID  int,   CustomerName  varchar(5),   CustomerContact int);
          insert into a values
          (1,             'John' ,            1234),
          (2,             'Anna' ,            5678),
          (3,             'Angel',            2468);
          
          drop table if exists b;
          create table b (ChannelName varchar(20),            OnlineStatus  varchar(3));
          insert into b values
          ('private-customer-1' ,    'YES'),
          ('private-customer-2' ,    'NO');
          
          Select b.OnlineStatus, a.CustomerName, a.CustomerContact 
          FROM   a
          join  b on  a.CustomerID = replace(b.ChannelName,'private-customer-','');
          
          Select b.OnlineStatus, a.CustomerName, a.CustomerContact 
          FROM   a
          join  b on  a.CustomerID = substring(b.ChannelName,18,length(b.channelname) -17)
          

          两个查询结果

          +--------------+--------------+-----------------+
          | OnlineStatus | CustomerName | CustomerContact |
          +--------------+--------------+-----------------+
          | YES          | John         |            1234 |
          | NO           | Anna         |            5678 |
          +--------------+--------------+-----------------+
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-05
            • 1970-01-01
            • 2013-01-30
            • 2010-12-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多