How to select ID column as null in MySQL?


Let us first create a table. The query to create a table is as follows

mysql> create table selectAllDemo
   - > (
   - > Name varchar(100),
   - > Age int
   - > );
Query OK, 0 rows affected (1.90 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into selectAllDemo values('John',25);
Query OK, 1 row affected (0.99 sec)
mysql> insert into selectAllDemo values('Carol',26);
Query OK, 1 row affected (0.42 sec)
mysql> insert into selectAllDemo values('Bob',30);
Query OK, 1 row affected (1.57 sec)
mysql> insert into selectAllDemo values('Sam',35);
Query OK, 1 row affected (0.26 sec)

Display all records from the table using select statement.

The query is as follows

mysql> select *from selectAllDemo;

The following is the output

+-------+------+
| Name  | Age  |
+-------+------+
| John  |   25 |
| Carol |   26 |
| Bob   |   30 |
| Sam   |   35 |
+-------+------+
4 rows in set (0.16 sec)

Here is the query to select only the ID column as NULL

mysql> select NULL AS Id,tbl.Name,tbl.Age from selectAllDemo tbl where Age=35;

The following is the output

+------+------+------+
| Id   | Name | Age  |
+------+------+------+
| NULL | Sam  | 35   |
+------+------+------+
1 row in set (0.07 sec)

Updated on: 30-Jul-2019

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements