如何在MySql

时间:2017-04-04 16:11:35

标签: mysql database concat

我正在尝试使用sqlfiddle v 5.6或MySQL workbench v5.7创建一个视图,以包含表CUSTOMERS中的所有信息,但将firstName和lastName连接为wholeName。 我尝试过使用以下内容:

CREATE VIEW v_customer AS SELECT *,
   CONCAT(CONCAT(lastName, ', '), firstName AS wholeName,
   FROM customers; 

CREATE VIEW v_customer AS SELECT customerID,
   CONCAT(CONCAT(lastName, ', '), firstName AS wholeName,
   ...(all other customer columns),
   FROM customers;

当省略CONCAT功能时,会创建视图。它让我相信我的语法有问题,但错误是在“FROM”行提出的。

3 个答案:

答案 0 :(得分:0)

您可以使用单个concat将两个以上的列或表达式连接在一起。

试试这个:

create view v_customer as
select *, 
    concat(lastname, ', ', firstname) as wholename,
from customers; 

答案 1 :(得分:0)

你只需要一个驯服,只需要将所有字符串添加到你需要的分隔符中

super.paintComponent(...)

答案 2 :(得分:0)

在收到这些答案之前,最终为我工作的代码......也是如此:

CREATE VIEW v_customer 
AS SELECT customerID,
   CONCAT(customers.firstName, ' ',customers.lastName) AS wholeName,
   street,
   apartment,
   city,
   state,
   zipCode,
   homePhone,
   mobilePhone,
   otherPhone
   FROM CUSTOMERS;