如何在MY SQL中运行select语句时生成自动增量值列?

时间:2014-09-01 04:55:28

标签: mysql

如何显示当我运行以查找我的表中的重复值此时我想在select语句中添加具有自动增量值的新列

SELECT * FROM tutorials_tbl
WHERE Lscno IN (
SELECT Lscno
FROM tutorials_tbl
GROUP BY Lscno
HAVING COUNT(Lscno) > 1
)ORDER BY Lscno

ID       Title          LSCNo
10       titl_14    000000/06/3/1944
19       titl_13    000000/06/3/19
561     titl_1      000000/10/125/1955
671     titl_1      000000/10/125/1955
1765    titl_1      000000/06/65/1976
1834    titl_1      000000/06/65/1976
1909    titl_1      000000/06/3/1971
1910    titl_1      000000/06/3/1971

 **Excepted output like** in MY SQL ?




 Add
NewCol    ID    Title   LSCNo
  1       10    titl_14 000000/06/3/1944
  2       19    titl_13 000000/06/3/19
  3       561   titl_1  000000/10/125/1955
  4       671   titl_1  000000/10/125/1955
  5       1765  titl_1  000000/06/65/1976
  6       1834  titl_1  000000/06/65/1976
  7       1909  titl_1  000000/06/3/1971
  8       1910  titl_1  000000/06/3/1971

2 个答案:

答案 0 :(得分:1)

创建变量。类似的东西:

SELECT @myCount:=@myCount+1 AS NewCol, ID, Title, LSCNo
FROM tutorials_tbl, (SELECT @myCount:= 0) t
WHERE Lscno IN (
    SELECT Lscno
    FROM tutorials_tbl
    GROUP BY Lscno
    HAVING COUNT(Lscno) > 1
)
ORDER BY Lscno

答案 1 :(得分:0)

尝试以下伪脚本:

select @a := ISNULL(@a, 0) + 1, *
from your_table;

这可以解决您的问题。