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

  • SEARCH
  • Home
  • 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 7962867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:27:56+00:00 2026-06-04T05:27:56+00:00

I have an UPDATE query where I explicitely reference the database, but MySQL still

  • 0

I have an UPDATE query where I explicitely reference the database, but MySQL still complains with the message: ERROR 1046 (3D000): No database selected.

Other queries that are similar of structure, but use an INSERT work fine. Other queries that only perform SELECTs also run fine.

To repeat the problem in a test case, try running these queries:

create table test.object1 (
    id_object1 int unsigned not null auto_increment,
    total int,
    weight int,
    dt datetime,
    primary key (id_object1)
) engine=InnoDB;

create table test.object2 (
    id_object2 int unsigned not null auto_increment,
    primary key (id_object2)
) engine=InnoDB;

create table test.score (
    id_object1 int unsigned not null,
    id_object2 int unsigned not null,
    dt datetime,
    score float,
    primary key (id_object1, id_object2),
    constraint fk_object1 foreign key (id_object1) references object1 (id_object1),
    constraint fk_object2 foreign key (id_object2) references object2 (id_object2)
) engine=InnoDB;

insert into test.object1 (id_object1, total, weight, dt) values (1, 0, 0, '2012-01-01 00:00:00');
insert into test.object1 (id_object1, total, weight, dt) values (2, 0, 0, '2012-01-02 00:00:00');

insert into test.object2 (id_object2) values (1);

insert into test.score (id_object1, id_object2, dt, score) values (1, 1, '2012-01-03 00:00:00', 10);
insert into test.score (id_object1, id_object2, dt, score) values (2, 1, '2012-01-04 00:00:00', 8);

update test.object1 p
join (
    select ur.id_object1, sum(ur.score * ur.weight) as total, count(*) as weight
    from ( 
        select lur.* 
        from ( 
            select s.id_object1, s.id_object2, s.dt, s.score, 1 as weight
            from test.score as s 
            join test.object1 as o1 using(id_object1) 
            where s.dt > o1.dt
            order by s.id_object1, s.id_object2, s.dt desc 
        ) as lur 
        group by lur.id_object2, lur.id_object1, date(lur.dt) 
        order by lur.id_object1, lur.id_object2 
    ) as ur 
    group by ur.id_object1
) as r using(id_object1) 
set 
    p.total = p.total + r.total, 
    p.weight = p.weight + r.weight, 
    p.dt = now();

Note: I’m running these queries from a PHP environment and I have NOT explicitely used mysql_select_db(‘test’), because I prefer not to and none of the other (many!) queries require it. I’m sure that using mysql_select_db would solve my issue, but I would like to know why exactly this particular query does not work.

For comparison sake: if you’d run this simpler query, also without using mysql_select_db, everything works fine:

update test.object1 set total=1, weight=1, dt=now() where id_object1=1;

I’ve searched to no avail. The only thing I found that came close, was this bug report: http://bugs.mysql.com/bug.php?id=28551 and especially that last (unanswered) message…

  • 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-06-04T05:27:57+00:00Added an answer on June 4, 2026 at 5:27 am

    You have fields named incorrectly, but even if you correct them, this is a bug in MySQL that won’t let you do it if you don’t have default database.

    update  test.object1 p
    join    (
            select  ur.id_object1, sum(ur.score * ur.weight) as total, count(*) as weight
            from    (
                    select  lur.*
                    from    (
                            select s.id_object1, s.id_object2, s.dt, s.score, 1 as weight
                            from   test.score as s
                            join   test.object1 as o1
                            using  (id_object1)
                            where  s.dt > o1.dt
                            order by
                                   s.id_object1, s.id_object2, s.dt desc
                            ) as lur
                    group by
                            lur.id_object1, lur.id_object1, date(lur.dt)
                    order by
                            lur.id_object1, lur.id_object1
                    ) as ur
            group by ur.id_object1
            ) as r
    USING   (id_object1)
    SET     p.total = p.total + r.total,
            p.weight = p.weight + r.weight,
            p.dt = now();
    

    The problem is specific to UPDATE with double-nested queries and no default database (SELECT or single-nested queries or default database work fine)

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

Sidebar

Related Questions

I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have an object model that's mapped to a database table. The update query
I have to update 2M*2rows in a mysql database. All the information is in
I have an Update Query Problem with jdbc and Access. The database name is
I have this SQL update query: UPDATE table1 SET table1.field1 = 1 WHERE table1.id
I have set up an update query which will update values entered into text
i have a file which contains a lot of update query and some other
I have one demo application to check select for update query public class Test
I have such a query: UPDATE `user` SET mail='abc@abc' WHERE id = IF((SELECT admin
I have a query that is ExcelQuery = Update [Sheet1$] +set CITIZEN_ID = #

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.