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 9195087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:33:25+00:00 2026-06-17T21:33:25+00:00

I have a table like the one below that currently has no values for

  • 0

I have a table like the one below that currently has no values for rating, lib_id or votes.

library

id | title | year | rating | votes  | lib_id |
---------------------------------------------
1  | book1 | 1999 |        |        |       |
2  | book2 | 2010 |        |        |       |
3  | book3 | 2009 |        |        |       |
4  | book4 | 2007 |        |        |       |
5  | book5 | 1987 |        |        |       |

I then have the classifications table which looks like this.

classifications

id   | title   | year | rating | votes  | lib_id |
---------------------------------------------
108  | book154 | 1929 |        |        |        |
322  | book23  | 2011 |        |        |        |
311  | book3   | 2009 |  9.3   |  4056  |  10876 |
642  | book444 | 2001 |        |        |        |
533  | book567 | 1981 |        |        |        |

It can happen that entries in the library table may not appear in the classifications table and vice-versa. There can also be the possibility that the title of the book is not unique. So what I want to do is go through each row in the library table, take the title and year columns, go to the classifications table and find the row that has these two values, retrieve the corresponding rating, votes and lib_id columns and update the entry in the library table.

I also want to use PDOs. Below is a non-working example of what i’m trying to achieve.

$update_vals_STH = 
$DBH->prepare(
   "UPDATE library SET lib_id=?, rating=?, votes=? 
    FROM (SELECT lib_id, rating, votes) 
    FROM classifications WHERE title=? AND year=?"; 

Any help would be appreciated. I’m quite new to MySQL and have been struggling with this one for a while.

  • 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-17T21:33:26+00:00Added an answer on June 17, 2026 at 9:33 pm

    You can join tables on update statement too.

    UPDATE  library a
            INNER JOIN classifications b
                ON  a.title = b.title AND
                    a.year = b.year
    SET     a.rating = b.rating,
            a.votes = b.votes,
            a.lib_id = b.lib_id
    // WHERE    clause                   // if you want to have extra condition.
    
    • SQLFiddle Demo

    UPDATE

    For better performance, you need to add indexes on the following field.

    ALTER TABLE library ADD INDEX (title, year);
    ALTER TABLE classifications ADD INDEX (title, year);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have a table like this one: id group value 1 GROUP A 0.641028
I have a table which looks like this one: | id | fk_book |
I have a table and I'd like to pull one row per id with
I have a table and one of the columns holds web addresses like: 'http://...'
I have a table, in one of the fields I stored a string like
I have a favorite table containing some fields like login_id,driver_id(One login_id may have many
I have a table of students that has some data about whether or not

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.