Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:19:17+00:00 2026-05-13T11:19:17+00:00

I’ve got the following MySQL query / subquery: SELECT id, user_id, another_id, myvalue, created,

  • 0

I’ve got the following MySQL query / subquery:

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

Given the criteria listed, the result for the subquery (old_id) should be null (no matches would be found in my table). Instead of MySQL returning null, it just seems to drop the “WHERE ParentUsersValue.user_id = UsersValue.user_id” clause and pick the first value that matches the other two fields. Is this a MySQL bug, or is this for some reason the expected behavior?

Update:

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'))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:19:18+00:00Added an answer on May 13, 2026 at 11:19 am

    This returns correct results (NULL) for me:

    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
    

    Could you please run this query:

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

    and make sure it returns 1?

    Note that your subquery does not filter on created, so the subquery can return values out of the range the main query defines.

    Update:

    This is definitely a bug in MySQL.

    Most probably the reason is that the access path chosen for UsersValues is index_intersect.

    This selects appropriate ranges from both indexes and build their intersection.

    Due to the bug, the dependent subquery is evaluated before the intersection completes, that’s why you get the results with the correct another_id but wrong user_id.

    Could you please check if the problem persists when you force PRIMARY scan on the 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
    

    Also, for this query you should create a composite index on (user_id, another_id, id) rather than two distinct indexes on user_id and another_id.

    Create the index and rewrite the query a little:

    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
    

    The user_id DESC, another_id DESC clauses are logically redundant, but they will make the index to be used for ordering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The relationship you describe is implied, but using it involves… May 13, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Not a JQuery solution, but this works: $("a.mylink").click(function() { $(this)[0].previousSibling.nodeValue… May 13, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Regina, abstract classes can define both abstract and virtual methods.… May 13, 2026 at 11:47 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.