Index: wp-includes/canonical.php
===================================================================
--- wp-includes/canonical.php	(revision 17365)
+++ wp-includes/canonical.php	(working copy)
@@ -391,24 +391,46 @@
 	if ( !get_query_var('name') )
 		return false;
 
-	$where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
+	$where = $wpdb->prepare( "post_name LIKE %s", like_escape( get_query_var('name') ) . '%' );
 
-	// if any of post_type, year, monthnum, or day are set, use them to refine the query
-	if ( get_query_var('post_type') )
-		$where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
-	if ( get_query_var('year') )
-		$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
-	if ( get_query_var('monthnum') )
-		$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
-	if ( get_query_var('day') )
-		$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
+	// if post_type is set, use it to refine query
+	if ( $post_type = get_query_var('post_type') )
+		$where .= $wpdb->prepare( " AND post_type = %s", $post_type );
+		
+	$possible_posts = $wpdb->get_results( "SELECT ID, post_date FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
+	$n = count( $possible_posts );
+	
+	if( 0 == $n )	// no match
+		return false;
+	if( 1 == $n )	// only one match
+		return get_permalink( $possible_posts[0]->ID );
+	
+	// more than one match - try to refine based on year, month, date
+	$y = get_query_var('year');
+	$m = get_query_var('monthnum');
+	$d = get_query_var('day');
+	
+	$best_fits = array();
+	foreach( $possible_posts as $key => $data ) {
+		$best_fits[$key] = 0;
+		if( $y && mysql2date( 'Y', $data->post_date ) == $y ) {
+			$best_fits[$key] ++;
+			if( $m && mysql2date( 'm', $data->post_date ) == $m ) {
+				$best_fits[$key] ++;
+				if( $d && mysql2date( 'j', $data->post_date ) == $d )
+					$best_fits[$key] ++;
+			}
+		}
+	}
+	
+	// if something came up trumps, use that, otherwise we're shooting in the dark
+	$max = max( $best_fits );
+	if( $max > 0 && count( array_keys( $best_fits, $max ) ) == 1 )
+		return get_permalink( $possible_posts[ array_search( $max, $best_fits ) ]->ID );
 
-	$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
-	if ( !$post_id )
-		return false;
-	return get_permalink($post_id);
+	return false;
 }
 
 add_action('template_redirect', 'redirect_canonical');
 
-?>
+?>
\ No newline at end of file
