12

I collect the same similar data with different type value based on id in one table:

 +------+---------------+-----------------+----------------+ 
 |  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   |
 +------+---------------+-----------------+----------------+ 

When I select the table based on the TransID of 1, I want to have a unique auto increment ID for the record based on the id of the table.

How to do this? So the result would be

 +------+---------------+-----------------+----------------+--------------- 
 |  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
 +------+---------------+-----------------+----------------+ ---------------

AND when I select to only a specific id of the table, for example the id of 4, it will give me the NewGeneratedID of 2, not 1.

 +------+---------------+-----------------+----------------+--------------- 
 |  4   |      1        |       12        |   22-03-2004   |       2
 +------+---------------+-----------------+----------------+ 
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Al Kasih
  • 887
  • 5
  • 24
  • 1
    I can think of no good reason why you'd want to do this. – Strawberry Apr 20 '16 at 09:32
  • I need to identify it to client side while merging all type with different identity of name. – Al Kasih Apr 20 '16 at 19:25
  • Any idea how to correctly achieve this, please? – Al Kasih Apr 22 '16 at 20:25
  • This look like something strange to do... Is this your actual problem? or some odd [workaround to other problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – It-Z Apr 29 '16 at 09:12
  • I don't know how to do it in mySQL, but in SQL Server there is a row_number() function when used in conjunction with the partition you can definitely solve this problem.. This answer might get you half-way there http://stackoverflow.com/questions/19589707/how-to-get-rownumber-with-partition-in-mysql – ewahner Apr 29 '16 at 19:03

6 Answers6

8

You can use following query for your requirement

    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;
Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
8

I have to warn you that the next query is inefficient but it can achieve what you need.

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;

If the process where you need it is critical on time you shouldn't use this query. However if you are only interested in getting one record you better use the following query which is efficient enough.

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;

UPDATE: The restriction aux.id <= t.id stablishes an order among the elements by counting how many elements with lesser id exists. for example the row with id 4 has one row with lesser id (1) for the same transaction and the row with id 7 has two rows with lesser id (1 and 4)

JCalcines
  • 1,236
  • 12
  • 25
5

If you are planing to add an column for that you could use MySQL INSERT TRIGGER something like bellow:

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

EDIT: Strategy is something like that, If you are using a physical column for NewGeneratedID then for each Insertion in your table count how many ROWs already exists for TransID(of new row) and set NewGeneratedID(of new row) by count + 1 . Remember, if you need to DELETE from table then there must need a AFTER DELETE trigger to make consistent NewGeneratedID.

rev_dihazum
  • 818
  • 1
  • 9
  • 19
5

Use the below query when you are selecting based on 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;

And For the id condition check use the below query

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 ;
Andrews B Anthony
  • 1,381
  • 9
  • 27
4

This is my suggestion for what I understand in your question:

If generated incremental ids for NewGeneratedID will be permanent:

  • Update the table and insert those newly generated ids
  • After that, you can now easily select with a given id and NewGeneratedID

If generated new ids for NewGeneratedID are just temporary:

  • After selecting and generating incremental ids, save them in a temporary table (See CREATE TABLE)
  • When data are saved in temporary table, you can now select with a given id and NewGeneratedID
  • Clean or delete data in temporary table after the session
rhavendc
  • 985
  • 8
  • 21
0

Isn't the id field already unique enough ? Why don't you use that ? If you absolutely need a new field you can just add one or any number to the id field and generate another value on every insert. i.e. New Field = ID + Constant.

antiskidwarpdrive
  • 321
  • 1
  • 2
  • 8