从唯一的id mysql生成自动增量值

Al *_*sih 11 mysql

我在一个表中基于id收集具有不同类型值的相同类似数据:

 +------+---------------+-----------------+----------------+ 
 |  id  |   TransID     |     Amount      |   InsertDate   |
 +------+---------------+-----------------+----------------+ 
 |  1   |      1        |       12        |   19-03-2004   |
 |  2   |      2        |       9         |   20-03-2004   |
 |  3   |      3        |       4         |   21-03-2004   |
 |  4   |      1        |       12        |   22-03-2004   |
 |  5   |      2        |       9         |   23-03-2004   |
 |  6   |      3        |       4         |   24-03-2004   |
 |  7   |      1        |       12        |   25-03-2004   |
 +------+---------------+-----------------+----------------+ 
Run Code Online (Sandbox Code Playgroud)

当我根据TransID为1选择表时,我希望根据表的id为记录提供唯一的自动增量ID.

这该怎么做?结果就是这样

 +------+---------------+-----------------+----------------+--------------- 
 |  id  |   TransID     |     Amount      |   InsertDate   | NewGeneratedID
 +------+---------------+-----------------+----------------+----------------- 
 |  1   |      1        |       12        |   19-03-2004   |       1
 |  4   |      1        |       12        |   22-03-2004   |       2
 |  7   |      1        |       12        |   25-03-2004   |       3
 +------+---------------+-----------------+----------------+ ---------------
Run Code Online (Sandbox Code Playgroud)

当我选择只有表的特定id时,例如id为4,它会给我NewGeneratedID 2,而不是1.

 +------+---------------+-----------------+----------------+--------------- 
 |  4   |      1        |       12        |   22-03-2004   |       2
 +------+---------------+-----------------+----------------+ 
Run Code Online (Sandbox Code Playgroud)

Fat*_*n P 8

您可以根据需要使用以下查询

    SELECT t.id,t.TransID ,t.Amount,t.InsertDate ,(@num:=@num+1) AS
 NewGeneratedID  FROM table1 t cross join (SELECT @num:=0) AS dummy 
where t.TransID=1  ORDER BY id;
Run Code Online (Sandbox Code Playgroud)


JCa*_*nes 8

我必须警告你,下一个查询效率低下,但它可以实现你所需要的.

SELECT t.id, t.TransID ,t.Amount, t.InsertDate, 
    (SELECT COUNT(id) FROM table1 AS aux 
     WHERE t.TransID = aux.TransID and aux.id <= t.id)
FROM table1 t 
WHERE t.TransID = 1  ORDER BY id;
Run Code Online (Sandbox Code Playgroud)

如果您需要它的过程在时间上是关键的,则不应使用此查询.但是,如果您只想获取一条记录,则最好使用以下有效的查询.

SELECT t.id, t.TransID , t.Amount, t.InsertDate, COUNT(*)
FROM table1 t inner join table1 aux where t.TransID = aux.TransID
WHERE aux.id <= t.id and t.id = 4
GROUP BY t.id, t.TransID , t.Amount, t.InsertDate;
Run Code Online (Sandbox Code Playgroud)

更新:限制aux.id <= t.id通过计算存在较少id的元素数来确定元素之间的顺序.例如,id为4的行对同一事务有一行id为较小的行(1),而id为7的行有两行id较小的行(1和4)


rev*_*zum 5

如果您计划添加一个列,您可以使用MySQL INSERT TRIGGER类似于下面的内容:

CREATE TRIGGER INSERT_Trigger BEFORE INSERT ON Your_Table
FOR EACH ROW BEGIN
  SET NEW.NewGeneratedID = (SELECT COUNT(*)+1 FROM Your_Table WHERE TransID = NEW.TransID);
END
Run Code Online (Sandbox Code Playgroud)

编辑:策略是这样的,如果你使用物理列NewGeneratedID然后为表中的每个插入计数已经存在多少ROW TransID(新行)和set NewGeneratedID(新行)按count + 1.请记住,如果需要从表中删除,则必须使用AFTER DELETE触发器才能保持一致NewGeneratedID.


And*_*ony 5

当您根据 TransactionID 选择时,请使用以下查询

SELECT t.id,t.TransID ,t.Amount,t.InsertDate ,(@num:=@num+1) AS
NewGeneratedID  FROM MyTable t 
cross join (SELECT @num:=0) AS dummy  where t.TransID=1  ORDER BY id;
Run Code Online (Sandbox Code Playgroud)

对于 id 条件检查,请使用以下查询

select t1.* from 
(SELECT t.id,t.TransID ,t.Amount,t.InsertDate,
(@num:=@num+1) AS  NewGeneratedID  
FROM MyTable t 
cross join (SELECT @num:=0) AS dummy  
where t.TransID=1  ORDER BY id) t1 
where t1.id=4 ;
Run Code Online (Sandbox Code Playgroud)