Fix error in MySQL “select ClientId,ClientName,ClientAge, from tablename”

MySQLMySQLi Database

The error occurs because we have a comma at the end of the column names, just before “from tablename’. Here is the error you may have got −

mysql> select ClientId,ClientName,ClientAge, from DemoTable1391;
ERROR 1064 (42000): 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 DemoTable1391' at line 1

To remove the error, as discussed above, you need to remove the last comma from the table. Let us first create a −

mysql> create table DemoTable1391
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20),
   -> ClientAge int
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1391(ClientName,ClientAge) values('Chris',21);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1391(ClientName,ClientAge) values('Bob',23);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1391(ClientName,ClientAge) values('David',24);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1391(ClientName,ClientAge) values('Mike',28);
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select −

mysql> select * from DemoTable1391;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | Chris      |        21 |
|        2 | Bob        |        23 |
|        3 | David      |        24 |
|        4 | Mike       |        28 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Following is the query to remove error i.e. this is the correct way of using SELECT statement and fetch records −

mysql> select ClientId,ClientName,ClientAge from DemoTable1391;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | Chris      |        21 |
|        2 | Bob        |        23 |
|        3 | David      |        24 |
|        4 | Mike       |        28 |
+----------+------------+-----------+
4 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 10:26:13
Advertisements