Count ID's in SQL
#1

Hi. I need some help with my SQL code... Im trying to get the last ID that was inserted in my database. It seems like the code Im using only counts the first 10 records.. I dont really know why. Maybe somebody knows why? I made 2 diffrent functions they both outputs the value: 9 (witch is the first 10 records in my database.) Somehow the code work if there is less then 10 records but not if there is more.. (This happens to both my functions) Did I do something wrong?

Simple version:
pawn Код:
stock GetLastIDSimple()
{
    new DBResult:qresult, string[128];
    qresult = db_query(GPSDB,"SELECT * FROM `System` ORDER BY `ID` DESC LIMIT 1");
    db_get_field(qresult,0,string,128);
    printf("Output %d", strval(string));
    db_free_result(qresult);
    return 1;
}
Reply
#2

Bump (12 hours+)
Reply
#3

If your sql ID's don't skip numbers you could use mysql_num_rows() to count how many rows are in the database, however if your numbers arn't in order this wouldn't work.
Reply
#4

First of all this is SQLite and not mysql. The numbers are only in order as long as I dont delete them from the database. Im just trying to get the highest number in the database but it only works for the first 10 records
Reply
#5

I think I got to bump this agian.. I cant find the problem.. I also tried to use SELECT MAX(id) but its still the same output.. Maybe anyone got another function that I could play around with?
Reply
#6

Well ******, the first thing works perfect (thanks a lot ) but if I lets say have 10 records and I delete number 7 so the ID's now will go from 0-6 then 8-9 then the code just returns 0.. Is there a way to fix that or should I just skip the whole function instead and try to find another way?
Reply
#7

Yes I did and it just returns 0 (both with and without "FROM `System`") Im just looking for a way to get the higest number from the ID's (Im sorry if that was not clear enough in my posts and I might even did it wrong from the start..) Like if I have ID 0-10 even if I delete number 5,6 or even 1 it should still give me the result of 10.. I hope you get what I mean
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)