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.
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.
Ran the script on a test db a couple times and looks to have done what I needed.