简体   繁体   English

为什么列的最大值永远不等于或大于10?

[英]Why is the maximum value of a column never equal to or greater than 10?

In my application I have table name is dataset_master . 在我的应用程序中,表名称为dataset_master And Table has field name is dataset_id , I want to get maximum value of this field so simple I fire following Query with code. 并且Table的字段名称为dataset_id ,我想获取此字段的最大值,因此我很简单地使用代码查询以下内容。

-(NSString *) getLastRowFromDataset_masterTableByProjectID:(NSString *) project_id
{
    NSString *dataset_id = @"";
    sqlite3_stmt *statement = NULL;
    NSString *query = [NSString stringWithFormat:@"SELECT MAX(dataset_id) FROM dataset_master where project_id = ?;"];

    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
    {
        sqlite3_bind_text(statement, 1, [project_id UTF8String], -1, SQLITE_TRANSIENT);

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            char *datasetIDChars = (char *) sqlite3_column_text(statement, 0);

            if(datasetIDChars != NULL)
                dataset_id  = [[NSString alloc] initWithUTF8String:datasetIDChars];

        }
    }

    return dataset_id;
}

Here project_id is require for me because I need to get dataset_id base on project_id . 这里对我来说project_id是必需的,因为我需要基于project_id获取dataset_id

All is well but when dataset_id is 10 or more then 10 then I can not get proper maximum value, i mean each time i get 9 . 一切都很好,但是当dataset_id10或 dataset_id 10时,我将无法获得适当的最大值,我的意思是每次获得9时

I don't know what is real problem I simple fire Query but I can not get proper answer. 我不知道什么是真正的问题,我只执行简单的查询,但无法获得正确的答案。

If it's a text field "9" will be greater than "10". 如果是文本字段,则“ 9”将大于“ 10”。 For an id field you should use an integer to get good comparisons. 对于id字段,您应该使用整数以获得良好的比较。

Then you can retrieve it with something like: 然后,您可以使用以下内容检索它:

NSNumber *id = [NSNumber numberWithLongLong:sqlite_column_int64(statement, i)];

When you create the sqlite table , use an INTEGER column and not TEXT. 创建sqlite表时 ,请使用INTEGER列,而不要使用TEXT。 Better yet, you can define the column INTEGER PRIMARY KEY and it will return the rowid of the table. 更好的是,您可以定义INTEGER PRIMARY KEY列,它将返回表的rowid。 More can be read here under ROWIDs and INTEGER PIMARY KEYS heading . 可以在ROWID和INTEGER PIMARY KEYS标题下阅读更多内容。 Since it's an alias for the rowid, you shouldn't need to assign a value when inserting data - it will get the next highest id. 由于它是rowid的别名,因此在插入数据时无需分配值-它会获得下一个最高ID。

If you do not want this column to be a unique id, then simply define the column as an integer and supply a value when inserting. 如果您不希望此列是唯一的ID,则只需将该列定义为整数并在插入时提供一个值。

Finally, if you're trying to get the max id, consider using sqlite3_last_insert_rowid after inserting. 最后,如果您要获取最大ID,请考虑在插入后使用sqlite3_last_insert_rowid It will return the last id from the rowid. 它将返回rowid中的最后一个id。

Use the following query but be aware this is a bit costly call: 使用以下查询,但是请注意,这是一个昂贵的调用:

NSString *query = [NSString stringWithFormat:@"SELECT MAX(CAST(dataset_id AS INTEGER)) FROM dataset_master where project_id = ?;"];

Else 其他

Changing datatype of dataset_id to integer should solve your issue. 将dataset_id的数据类型更改为整数应该可以解决您的问题。

For Ranju Patel's query: 对于Ranju Patel的查询:

  • Why MAX function would not work with TEXT datatype? 为什么MAX函数不能与TEXT数据类型一起使用?

In case of TEXT datatype, the sorting order would be ASCII sorting order, 如果是TEXT数据类型,则排序顺序为ASCII排序顺序,

where in "9" > "10" , "09" < "10" , [Ref: http://en.wikipedia.org/wiki/ASCII#Order], Hence the MAX function would not work if the data stored is "9", "10". “ 9”>“ 10”“ 09” <“ 10” ,[Ref:http://en.wikipedia.org/wiki/ASCII#Order]中的位置,因此,如果存储的数据为“ 9”,“ 10”。

on the other hand, 另一方面,

if the data sotred is "09", "10", and your query is: 如果存储的数据是“ 09”,“ 10”,并且您的查询是:

NSString *query = [NSString stringWithFormat:@"SELECT MAX(dataset_id) FROM dataset_master where project_id = ?;"];

the result you would get is "10". 您将得到的结果是“ 10”。

  • Would my answer be valid for long time? 我的答案长期有效吗?

It would work if you don't have too many data, else it would slow you down, Hence I have mentioned it as: its very costly to call CAST function in the raw SELECT query. 如果您没有太多数据,它将起作用,否则它将使您变慢,因此,我将其提及为:在原始SELECT查询中调用CAST函数非常昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM