MySQL子查询返回错误的结果?

时间:2010-01-20 14:32:37

标签: sql mysql subquery

我有以下MySQL查询/子查询:

SELECT id, user_id, another_id, myvalue, created, modified,
(
    SELECT id 
    FROM users_values AS ParentUsersValue
    WHERE ParentUsersValue.user_id = UsersValue.user_id
    AND ParentUsersValue.another_id = UsersValue.another_id 
    AND ParentUsersValue.id < UsersValue.id 
    ORDER BY id DESC 
    LIMIT 1
) AS old_id

FROM users_values AS UsersValue
WHERE created >= '2009-12-20' 
AND created <= '2010-01-21' 
AND user_id = 9917
AND another_id = 23

根据列出的条件,子查询(old_id)的结果应该为null(在我的表中找不到匹配项)。它似乎丢弃了“WHERE ParentUsersValue.user_id = UsersValue.user_id”子句并选择与其他两个字段匹配的第一个值,而不是MySQL返回null。这是一个MySQL错误,还是由于某种原因这是预期的行为?

更新

CREATE TABLE users_values (
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) DEFAULT NULL,
    another_id int(11) DEFAULT NULL,
    myvalue double DEFAULT NULL,
    created datetime DEFAULT NULL,
    modified datetime DEFAULT NULL,
    PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=2801 DEFAULT CHARSET=latin1

EXPLAIN EXTENDED

id  select_type table   type    possible_keys   key key_len ref rows    filtered    Extra
1   PRIMARY UsersValue  index_merge user_id,another_id  user_id,another_id  5,5 NULL    1   100.00  Using intersect(user_id,another_id); Using where
2   DEPENDENT SUBQUERY  ParentUsersValue    index   PRIMARY,user_id,another_id  PRIMARY 4   NULL    1   100.00  Using where

EXPLAIN EXTENDED Warning 1003

select `mydb`.`UsersValue`.`id` AS `id`,`mydb`.`UsersValue`.`user_id` AS `user_id`,`mydb`.`UsersValue`.`another_id` AS `another_id`,`mydb`.`UsersValue`.`myvalue` AS `myvalue`,`mydb`.`UsersValue`.`created` AS `created`,`mydb`.`UsersValue`.`modified` AS `modified`,(select `mydb`.`ParentUsersValue`.`id` AS `id` from `mydb`.`users_values` `ParentUsersValue` where ((`mydb`.`ParentUsersValue`.`user_id` = `mydb`.`UsersValue`.`user_id`) and (`mydb`.`ParentUsersValue`.`another_id` = `mydb`.`UsersValue`.`another_id`) and (`mydb`.`ParentUsersValue`.`id` < `mydb`.`UsersValue`.`id`)) order by `mydb`.`ParentUsersValue`.`id` desc limit 1) AS `old_id` from `mydb`.`users_values` `UsersValue` where ((`mydb`.`UsersValue`.`another_id` = 23) and (`mydb`.`UsersValue`.`user_id` = 9917) and (`mydb`.`UsersValue`.`created` >= '2009-12-20') and (`mydb`.`UsersValue`.`created` <= '2010-01-21'))

2 个答案:

答案 0 :(得分:0)

您是否尝试仅运行子查询以查看是否获得了正确的结果?你能告诉我们users_values表的架构吗?

另外,请尝试使用SELECT id

替换子查询中的SELECT ParentUsersValue.id

答案 1 :(得分:0)

这会为我返回正确的结果(NULL):

CREATE TABLE users_values (id INT NOT NULL PRIMARY KEY, user_id INT NOT NULL, another_id INT NOT NULL, created DATETIME NOT NULL);

INSERT
INTO    users_values VALUES (1, 9917, 23, '2010-01-01');

SELECT  *,
        (
        SELECT  id
        FROM    users_values AS ParentUsersValue
        WHERE   ParentUsersValue.user_id = UsersValue.user_id
                AND ParentUsersValue.another_id = UsersValue.another_id
                AND ParentUsersValue.id < UsersValue.id
        ORDER BY id
                DESC
        LIMIT 1
        ) AS old_id
FROM    users_values AS UsersValue
WHERE   created >= '2009-12-20'
        AND created <= '2010-01-21'
        AND user_id = 9917
        AND another_id = 23

请你运行这个查询:

SELECT  COUNT(*)
FROM    users_values AS UsersValue
WHERE   user_id = 9917
        AND another_id = 23

并确保它返回1

请注意,子查询不会对created进行过滤,因此子查询可以返回超出主查询定义范围的值。

<强>更新

这绝对是MySQL中的错误。

最有可能的原因是为UsersValues选择的访问路径为index_intersect

这将从两个索引中选择适当的范围并构建它们的交集。

由于该错误,在交集完成之前会对依赖子查询进行评估,这就是为什么您使用正确的another_id但错误user_id得到结果。

您可以在PRIMARY强制UsersValues扫描时检查问题是否仍然存在:

SELECT  *,
        (
        SELECT  id
        FROM    users_values AS ParentUsersValue
        WHERE   ParentUsersValue.user_id = UsersValue.user_id
                AND ParentUsersValue.another_id = UsersValue.another_id
                AND ParentUsersValue.id < UsersValue.id
        ORDER BY id
                DESC
        LIMIT 1
        ) AS old_id
FROM    users_values AS UsersValue FORCE INDEX (PRIMARY)
WHERE   created >= '2009-12-20'
        AND created <= '2010-01-21'
        AND user_id = 9917
        AND another_id = 23

此外,对于此查询,您应该在(user_id, another_id, id)而不是user_idanother_id上的两个不同索引上创建综合索引。

创建索引并稍微重写查询:

SELECT  *,
        (
        SELECT  id
        FROM    users_values AS ParentUsersValue
        WHERE   ParentUsersValue.user_id = UsersValue.user_id
                AND ParentUsersValue.another_id = UsersValue.another_id
                AND ParentUsersValue.id < UsersValue.id
        ORDER BY
                user_id DESC, another_id DESC, id DESC
        LIMIT 1
        ) AS old_id
FROM    users_values AS UsersValue
WHERE   created >= '2009-12-20'
        AND created <= '2010-01-21'
        AND user_id = 9917
        AND another_id = 23

user_id DESC, another_id DESC子句在逻辑上是多余的,但它们会使索引用于排序。