将auto_increment serial_id作为新col添加到select语句

时间:2014-05-25 09:26:54

标签: mysql sql join

这是我的问题:

select * from a
JOIN B
ON
a.id=B.id
JOIN C
ON
B.id=C.id

如何在开头添加一个名为' serial_id'哪个是自动增量?

我见过这里的例子:MySQL query to select results with auto increment as a new column added in the result

但是当我加入select语句时,解决方案不起作用。

1 个答案:

答案 0 :(得分:1)

试试这种方式

SELECT *
,@a:= @a +1 `serial_no`
 FROM a
JOIN B
ON
a.id=B.id
JOIN C
ON
B.id=C.id
JOIN (SELECT @a:=0) t

SELECT *
,@a:= @a +1 `serial_no`
 FROM a , (SELECT @a:=0) t
JOIN B
ON
a.id=B.id
JOIN C
ON
B.id=C.id