Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with my SQL code on CONCAT?

CREATE VIEW ‘CustomerView’ AS SELECT
FROM ‘Customer’
ORDER BY ‘CustomerID’, CONCAT(‘FirstName,’ ‘ ’, ‘LastName’) AS ‘CustomerName’, ‘StreetAddress’, ‘Apt’, ‘City’, ‘State’, ‘ZipCode’, ‘HomePhone’, ‘MobilePhone’, ‘OtherPhone’
;

I am getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ‘Customer’ ORDER BY ‘CustomerID’, CONCAT(‘FirstName,’ ‘ ’, ' at line 2

like image 391
Colleen Norton Avatar asked Feb 17 '26 12:02

Colleen Norton


2 Answers

It looks like you're using smart-quotes around your table and column identifiers. You should never use smart-quotes in code. There is no coding language that uses those characters.

You wouldn't use single-quotes around identifiers either. MySQL uses back-quotes by default for identifiers. Single-quotes are used for string literals or date literals in SQL.

Back-quotes look like this:

``

Single-quotes look like this:

''

Also you seem to have put your columns after the ORDER BY clause. I think you need to learn the syntax of SELECT.

I also saw an error in your CONCAT(). You got a comma inside the quotes when it should have been outside the quotes. You have this:

CONCAT(‘FirstName,’ ‘ ’, ‘LastName’)

It should be like this:

CONCAT(`FirstName`, ' ', `LastName`)

You would write the view like this:

CREATE VIEW `CustomerView` AS 
  SELECT CONCAT(`FirstName`, ' ', `LastName`) AS `CustomerName`, 
  `StreetAddress`, 
  `Apt`, 
  `City`, 
  `State`, 
  `ZipCode`, 
  `HomePhone`, 
  `MobilePhone`, 
  `OtherPhone`
FROM `Customer`
ORDER BY `CustomerID`;

The back-quotes are optional in your example. They are needed if your identifiers conflict with SQL reserved words or if the identifiers contain spaces or special characters (punctuation, international symbols, etc.). None of these cases apply in your example. So you can write it without back-quotes just fine:

CREATE VIEW CustomerView AS 
  SELECT CONCAT(FirstName, ' ', LastName) AS CustomerName, 
  StreetAddress, 
  Apt, 
  City, 
  State, 
  ZipCode, 
  HomePhone, 
  MobilePhone, 
  OtherPhone
FROM Customer
ORDER BY CustomerID;
like image 196
Bill Karwin Avatar answered Feb 20 '26 03:02

Bill Karwin


You missed a comma

ORDER BY ‘CustomerID’, CONCAT(‘FirstName,’, ‘ ’, ‘LastName’) AS  .....

Another way to write this more simple is adding the space after Firstname:

ORDER BY ‘CustomerID’, CONCAT(‘FirstName, ’, ‘LastName’) AS  .....

update

Also:

CREATE VIEW ‘CustomerView’ AS SELECT **"......"**

You didn't say what columns you want to select, if you want to select all columns, just add *

CREATE VIEW CustomerView AS 
SELECT CONCAT(FirstName, ' ', LastName) AS CustomerName,
...
...
FROM `Customer`
ORDER BY `CustomerID`;
like image 43
A Monad is a Monoid Avatar answered Feb 20 '26 03:02

A Monad is a Monoid