Index: src/PhotoStore.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/PhotoStore.cs,v
retrieving revision 1.72
diff -u -p -r1.72 PhotoStore.cs
--- src/PhotoStore.cs	2 Nov 2005 08:33:15 -0000	1.72
+++ src/PhotoStore.cs	11 Nov 2005 23:15:33 -0000
@@ -1132,13 +1132,27 @@ public class PhotoStore : DbStore {
 		//        photos.name,
 		//        photos.description,
 		//        photos.default_version_id
-		//                  FROM photos, photo_tags
-		//                  WHERE photos.id = photo_tags.photo_id
-		// 		          AND (photo_tags.tag_id = tag1
-		//			       OR photo_tags.tag_id = tag2
-		//                             OR photo_tags.tag_id = tag3 ...)
-		//                  GROUP BY photos.id
-		
+		//            FROM photos
+		//             [[ WHERE photos.time >= start AND photos.time < end ]] 
+		//             [[ WHERE photos.id NOT IN (
+		//               SELECT photo_id 
+	        //                  FROM photo_tags 
+	        //                   WHERE tag_id = hidden) ]]
+		//             WHERE photos.id IN (
+		//              SELECT photo_id 
+ 		//                 FROM photo_tags 
+		//                  WHERE tag_id = tag1
+		//              INTERSECT
+		//              SELECT photo_id 
+ 		//                 FROM photo_tags 
+		//                  WHERE tag_id = tag2
+		//              INTERSECT
+		//              SELECT photo_id 
+ 		//                 FROM photo_tags 
+		//                  WHERE tag_id = tag3 
+		//              ...)
+		//            ORDER BY photos.time
+
 		StringBuilder query_builder = new StringBuilder ();
 		query_builder.Append ("SELECT photos.id,                          " +
 				      "       photos.time,                        " +
@@ -1169,22 +1183,22 @@ public class PhotoStore : DbStore {
 					continue;
 				
 				if (first) {
-					query_builder.Append (String.Format ("{0} photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id IN (",
+					query_builder.Append (String.Format ("{0} photos.id IN (SELECT photo_id FROM photo_tags WHERE tag_id = ",
 									     hide || range != null ? " AND " : " WHERE "));
 				}
 				
-				query_builder.Append (String.Format ("{0}{1} ", first ? "" : ", ", t.Id));
+				query_builder.Append (String.Format ("{0}{1} ", first ? "" : "INTERSECT SELECT photo_id FROM photo_tags WHERE tag_id = ", t.Id));
 				
 					first = false;
 			}
 			
 			if (!first)
-				query_builder.Append (")) ");
+				query_builder.Append (") ");
 		}
 		
 		query_builder.Append ("ORDER BY photos.time");
 		query = query_builder.ToString ();
-		
+		// DEBUG: Console.WriteLine(query);	
 		return Query (query);
 	}
 
