2017-04-04 99 views
0

我試圖使用sqlfiddle v 5.6或MySQL Workbench v5.7創建視圖以包含來自表CUSTOMERS的所有信息,但將firstName和lastName連接爲wholeName。 我使用以下嘗試:如何在MySql中創建視圖時使用select *和concat

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」行提出。

回答

0

您可以使用單個concat將兩個以上的列或表達式連接在一起。

試試這個:

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

謝謝。我發佈了對我有用的東西,但是這也適用。 – Halston

0

您shoudl Concat的一個溫順只有將所有的字符串的separatore你需要

CREATE VIEW v_customer AS SELECT *, 
    CONCAT(lastName, ', ', firstName) AS wholeName, 
    FROM customers; 
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; 
+0

這或多或少是我發佈的。 – GurV

相關問題