我的数据库设置有4个表。所有表都有相同的列数,它们的列名略有不同。如果它是ZCARBOHYDRATES则在一个表中,然后在另一个表中,它是ZCARBOHYDRATES_PER_SERVING。
在视图控制器上,我有一个搜索栏。在搜索栏中键入内容时,搜索查询将起作用,从数据库中检索数据并填充表格视图。
到目前为止,搜索查询的工作方式如下:
如果用户输入莫扎里拉奶酪(全脂牛奶),那么
现在1.的结果是“联合”所有包含Mozzarella'union'的记录包含'Cheese''union'的所有记录包含'Whole''union'的所有记录包含''奶”。
代码:
-(NSArray *)getFilteredRecordsUsingSearchText:(NSString *)searchText
{
NSMutableArray *searchArr = [[NSMutableArray alloc]init];
//for first table
NSMutableString *queryForRecipe = [NSMutableString stringWithFormat:@" select ZCODE as code , ZRECIPE_TITLE as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVINGS as servingSize , ZSERVING_SIZE_DESCRIPTION_1 as servingUnit , 'ZRECIPES' as TableName from ZRECIPES where title like '%%%@%%' ",searchText];//retrieving data according to point 1
NSArray *seprateSearchText = [searchText componentsSeparatedByString:@" "];
for (NSString *str in seprateSearchText) {
if (str.length>2) {
[queryForRecipe appendFormat:@" union select ZCODE as code , ZRECIPE_TITLE as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVINGS as servingSize , ZSERVING_SIZE_DESCRIPTION_1 as servingUnit , 'ZRECIPES' as TableName from ZRECIPES where title like '%%%@%%' ",str];
}
}//retrieving data according to point 2
//for second table
NSMutableString *queryForAddedRecipe=[NSMutableString stringWithFormat:@" select ZCODE as code , ZRECIPE_TITLE as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVINGS as servingSize , ZSERVING_SIZE_DESCRIPTION_1 as servingUnit , 'ZADDED_RECIPE' as TableName from ZADDED_RECIPE where title like '%%%@%%' ",searchText];//retrieving data according to point 1
for (NSString *str in seprateSearchText) {
if (str.length>2) {
[queryForAddedRecipe appendFormat:@" union select ZCODE as code , ZRECIPE_TITLE as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVINGS as servingSize , ZSERVING_SIZE_DESCRIPTION_1 as servingUnit , 'ZRECIPES' as TableName from ZRECIPES where title like '%%%@%%' ",str];
}
}//retrieving data according to point 2
//for third table
NSMutableString *queryForUSDAFood=[NSMutableString stringWithFormat:@" select ZCODE as code , ZFOOD_ITEM as title , ZFAT as fat , ZFIBER as fiber , ZPROTEIN as protien , ZSUGARS as sugar , ZCARBOHYDRATE as carbs , ZCALORIES as calories , ZSERVING_DESC_1 as servingSize , ZSERVING_SIZE_DESC_1 as servingUnit ,'ZUSDA_FOODS' as TableName from ZUSDA_FOODS where title like '%%%@%%' ",searchText];//retrieving data according to point 1
for (NSString *str in seprateSearchText) {
if (str.length>2) {
[queryForUSDAFood appendFormat:@" union select ZCODE as code , ZFOOD_ITEM as title , ZFAT as fat , ZFIBER as fiber , ZPROTEIN as protien , ZSUGARS as sugar , ZCARBOHYDRATE as carbs , ZCALORIES as calories , ZSERVING_DESC_1 as servingSize , ZSERVING_SIZE_DESC_1 as servingUnit ,'ZUSDA_FOODS' as TableName from ZUSDA_FOODS where title like '%%%@%%' ",str];
}
}//retrieving data according to point 2
//for fourth table
NSMutableString *queryForAddedFood=[NSMutableString stringWithFormat:@" select ZCODE as code , ZFOOD_ITEM as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVING_SIZE_1 as servingSize , ZSERVING_UNIT_1 as servingUnit , 'ZADDED_FOOD_ITEMS' as TableName from ZADDED_FOOD_ITEMS where title like '%%%@%%' ",searchText];//retrieving data according to point 1
for (NSString *str in seprateSearchText) {
if (str.length>2) {
[queryForAddedFood appendFormat:@" union select ZCODE as code , ZFOOD_ITEM as title , ZFAT_PER_SERVING as fat , ZFIBER_PER_SERVING as fiber , ZPROTEIN_PER_SERVING as protien , ZSUGARS_PER_SERVING as sugar , ZCARBOHYDRATES_PER_SERVING as carbs , ZCALORIES_PER_SERVING as calories , ZSERVING_SIZE_1 as servingSize , ZSERVING_UNIT_1 as servingUnit , 'ZADDED_FOOD_ITEMS' as TableName from ZADDED_FOOD_ITEMS where title like '%%%@%%' ",str];
}
}//retrieving data according to point 2
NSString *combineQuery=[NSString stringWithFormat:@" %@ union %@ union %@ union %@ ORDER BY title ASC ",queryForRecipe,queryForAddedRecipe,queryForUSDAFood,queryForAddedFood];
........
........
// rest就是准备sql语句......
现在需要做什么?
现在必须更改搜索查询以满足以下条件:
1.如果搜索词包含多个单词,例如“鸡胸肉”搜索结果应该包含项目标题中的两个单词,包括复数形式如“鸡胸肉”。如果搜索是“苹果”等,也可以是“苹果”。
2.Punctuation marks(', - ,/,...)和文章(the,and,an)应该被忽略(例如,“鸡(乳房或翅膀......)将首先返回所有3个单词。接下来,任何2个字。最后任意一个字。
我正在寻求帮助以形成符合上述标准的查询(尤其是难以满足第2点。)。