Question: I have a MySQL database from which a view is created. Is is possible to add an auto-incrementing id for each row in the view?

I tried

```
CREATE ALGORITHM=UNDEFINED DEFINER=`database_name`@`%` SQL SECURITY DEFINER VIEW `MyView` AS 
set @i = 0;
select  @i:=@i+1 as `id`
        ...

```

but that doesn't work in a View.

Answer: 
Try this,

```
CREATE VIEW view_wp_postmeta AS(
SELECT (
  SELECT count( meta_id ) +1
  FROM wp_postmeta 
  AS vtmp     
  WHERE vtmp.meta_id < pm.meta_id
) AS vtmp_id, pm. *
FROM wp_postmeta AS pm
ORDER BY pm.meta_id DESC
)

```

In WordPress meta\_id = AUTO\_INCREMENT in wp\_postmeta TABLE, i create a VIEW for this, in which view\_id behaves like AUTO\_INCREMENT in DESC order.

For example,
In **TABLE** wp\_postmeta -> 
meta\_id [3,5,6,10,2,11]
 which would appear 
in **VIEW** view\_wp\_postmeta -> 
view\_id [6,5,4,3,2,1]
if you prefer this solution within an one-liner check this:

```sql
SELECT @autoIncrementNumber:=@autoIncrementNumber + 1 AS Num, 
columnName FROM viewName,(select @autoIncrementNumber:=0) as temp limit 0,8;

```