简体   繁体   中英

How do I use select * and concat while creating a view in MySql

I'm trying to create a view using sqlfiddle v 5.6 or MySQL workbench v5.7 to include all information from a table CUSTOMERS, but concatenating firstName and lastName as wholeName. I've tried using the following:

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

and

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

When leaving out the CONCAT function, the view is created. It leads me to believe there is something wrong with my syntax, but the error is brought up at the "FROM" line.

You can use single concat to concat more than two column or expressions together.

Try this:

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

You shoudl concat one tame only adding all the string an separatore you need

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

The code that eventually worked for me, prior to receiving these answers...which also worked, was:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM