user1525369
user1525369

Reputation:

How can I get max value of specific field in Table?

As my Question says that how can i get maximum value from Table?

In my apps. I have table name dataset_master

And table has field name is dataset_id, it is add manually as auto_inc.

So, First time when no record is inserted in Table and when I insert first record then I add dataset_id is 1. (this is only first time)

And Then after insert next record for dataset_id i fire query for get max value of dataset_id and I insert dataset_id +1. (This is for next record and so on..)

In my case I use following Query for get maximum dataset_id.

SELECT MAX(dataset_id) FROM dataset_master where project_id = 1

Here in my application I want to get maximum value of field name is dataset_id from dataset_master table.

This Query properly work when I insert record to dataset_master table each time I get proper maximum number of dataset_id. But when I delete record in sequins such like (1 to 5 from 10) in table and after I insert new record then I got each time last maximum number such like

if my table has 10 record then my dataset_id is 1 to 10; When I delete record such like 1 to 5 then remains 6 to 10 record and also dataset_id in Table. And then after I insert new record then each time I got 10 (maximum Number) so each time new record has dataset_id is dataset_id + 1 so 11.

What is problem I don't know (may be mistake in Query ?), please give your suggestion.

Upvotes: 1

Views: 144

Answers (2)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11181

I think the problem is not in your query, but in your insert. Do you force dataset_id when inserting new rows?

Upvotes: 0

hd1
hd1

Reputation: 34677

You need to reset the sequence in the sqlite_sequence table. I'd advise you not to worry about this though, as by the time it becomes a problem, this will be the least of your headaches.

Upvotes: 1

Related Questions