标题可能听起来很奇怪,但我会尝试尽可能简单地解释问题。让我们以示例开始吧。我有一个有2列的表--id,ip。我们说我有3行,内容为2,3,5。现在,我需要在id 1和5之间得到任何行,这显然是1和4.目前我仍然坚持这个查询:
// ModelClass.h
@property(retain,nonatomic) NSMutableArray *productListArray;
-(void) getProductInfoWs:(WebService *) webServiceReference :(GlobalValues *) valueReference :(NSString *) categoryId :(UIView *) overlayView;
// ModelClass.m
-(void) getProductInfoWs:(WebService *) webServiceReference :(GlobalValues *) valueReference :(NSString *) categoryId :(UIView *) overlayView{
productListArray=[localstoreModel getProductInfo:1];
if([[ResponseData objectForKey:@"status"] boolValue]==1){
productListArray=[[ResponseData objectForKey:@"result"][@"category_products"] mutableCopy];
}
else{
if([httpResponse statusCode]==200 && [[ResponseData objectForKey:@"status"] boolValue]==0){
[webServiceReference AlertMessage:[ResponseData objectForKey:@"errormsg"]];
}
else{
[webServiceReference AlertMessage:@"2"];
}
}
//CustomAlert.m
-(void) AlertMessage:(NSString *) message{
UIAlertView *alertBox = [[UIAlertView alloc] initWithTitle:LocalizedString(@"Information!")
message:message
delegate:nil
cancelButtonTitle:LocalizedString(@"OK")
otherButtonTitles:nil];
[alertBox show];
}
答案 0 :(得分:2)
听起来很奇怪,但这就是很多人所做的。
创建一个帮助表。将它用于左连接
create table amfn
( -- All My Favorite Numbers
id int auto_increment primary key,
theWhat char(1) null
)engine=MyIsam; -- <----- somewhat important
insert amfn(theWhat) values (null),(null),(null),(null),(null),(null),(null),(null),(null),(null); -- 10
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
insert amfn(theWhat) select theWhat from amfn;
select count(*),min(id),max(id) from amfn;
+----------+---------+---------+
| count(*) | min(id) | max(id) |
+----------+---------+---------+
| 1310720 | 1 | 1310720 |
+----------+---------+---------+
1 row in set (0.00 sec)
create table votes
( question_id int not null,
ip varchar(20) not null
);
insert votes (question_id,ip) values (1,'xxxx'),(2,'1.1.1.1'),(3,'1.1.1.1'),(4,'1.6.1.1'),(5,'1.1.1.1');
select a.id,v.question_id,v.ip
from amfn a
left join votes v
on v.question_id=a.id and v.ip='1.1.1.1'
where a.id between 1 and 5 and v.question_id is null;
+----+-------------+------+
| id | question_id | ip |
+----+-------------+------+
| 1 | NULL | NULL |
| 4 | NULL | NULL |
+----+-------------+------+
2 rows in set (0.00 sec) <------------- boy that is fast
我上面创建5242880行的方法,23.5
秒。康拉德的方法,168.5
秒。我会坚持我的方法:&gt;
答案 1 :(得分:1)
好的,我找到了解决方案 -
我为每个question_id和所有填充了NULL的ip字段创建了新列,之后我在SQL查询中使用了这个逻辑:
SELECT question_id
FROM `votes`
WHERE (ip = 'NULL' OR ip = ?)
GROUP BY question_id
HAVING COUNT(*) - COUNT(DISTINCT question_id) = 0
ORDER BY RAND()
LIMIT 1
结果我得到一个尚未使用的随机行,因为它搜索重复项,如果找到任何重复项,则会从搜索中删除它们。我希望我写下这个可以理解的