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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:29:39+00:00 2026-06-12T08:29:39+00:00

in a quandary here.. I’ve got one table, let’s call it bookshelves. The table

  • 0

in a quandary here..

I’ve got one table, let’s call it bookshelves. The table has these fields:

[shelf_id, title, latest_books]

Then I’ve got a table for books with the fields:

[book_id, shelf_id, title, date_added]

Each book is assigned to a shelf. Here’s what I want to do…

for every bookshelf, look in books for books assigned to that bookshelf, grab the book_id from those books and feed them into the bookshelves field ‘latest_books’ separated by commas. To complicate things further(?) I only want five book_id’s per shelf. I could do with just one from each if that made it easier..here’s the query I can use to manually do this one by one:

update bookshelves
set latest_books = 
    (select book_id from books 
        where shelf_id = 1 
        order by date_added DESC 
        limit 0,1)
where shelf_id = 1;

Both the bookshelves and books tables have tens of thousands of records so..I can do it one by one but I’ll be at it all year.

I’m working in MySQL on CentOS. I’m cool using scripting or whatever.

  • 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-12T08:29:40+00:00Added an answer on June 12, 2026 at 8:29 am

    Okay so I figured it out..it helped to type it out in detail here…

    I exported the bookshelves ids to a csv then put them into an array in a bash script.

    shelves=(
    1
    ...
    ...
    99999
    )
    
    for i in "${shelves[@]}"
    do
            query='update bookshelves set latest_books = (select book_id from books where shelf_id = '$i' order by date_addet desc limit 0,1) where latest_books = "" and shelf_id = '$i' ;'
            mysql -uxxxx  -pxxx database_name -e "$query"
    
    done
    

    Ran the script on a test db a couple times and looks to have done what I needed.

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

Sidebar

Related Questions

I have a bit of a quandary. I need to call a function inside
Let's say I have this document. <a:Root> <a:A> <title><a:B/></title> <a:C> <item><a:D/></item> </a:C> </a:A> </a:Root>
Here's my quandary. I have a variable that contains a paragraph of text, and
This is my quandary. Let me just set the scene: I have a grid
I have a quandary. My web application (C#, .Net 3.0, etc) has Themes, CSS
Here is my problem: I have a list of Python dictionaries of identical form,
This scoping problem seems like the type of C++ quandary that Scott Meyers would
I've got a rails app with a functioning restful_authentication system but I need to
I am facing a bit of a quandary, I need to replace a new
I often find myself in a quandary in where to put serialisation code for

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.