Search code examples
mysqlunique

Mysql statement with unique id created


I would like to ask how the mysql statement should be written in which unique ids are created in individual result rows (but actually not existed in the table)

For example, the content of table 'ABC' is:

name type

John A

Mary B

Peter C

Fred A

Gary C

Susan D

Alan A

When mysql statement SELECT * from ABC where type = 'A' is used, the result is

name type

John A

Fred A

Alan A

But I would like to put unique ids in each row of the mysql result, but the ids are NOT actually put in the table. For example,

(newid) name type

1 John A

2 Fred A

3 Alan A

Then the row can be further searched by using the following mysql statement.

SELECT * from ABC WHERE (newid) = '1'

The result is:

(newid) name type

1 John A

Thanks.


Solution

  • You can add an autoincrement column

    ALTER TABLE your_table ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;