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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:35+00:00 2026-06-06T19:20:35+00:00

I have 3 MySQL tables: shelf ————— id project_id label books ————— id project_id

  • 0

I have 3 MySQL tables:

shelf
---------------
id
project_id
label


books
---------------
id
project_id
label


booksOnShelves
---------------
id
shelf_id
book_id
project_id
user_id

I need to display what books are on what shelf in a table format, something like:

History     Fiction
-------     --------
book 1      book 4
book 2      book 5 
book 3

So far I’ve got only this:

  SELECT 
        shelf.label,
        books.label
    FROM booksOnShelves
    LEFT JOIN shelf ON (shelf.id = booksOnShelves.shelf_id)
    LEFT JOIN books ON (books.id = booksOnShelves.book_id) 
    WHERE booksOnShelves.project_id = ".$_POST['pid']."
    AND booksOnShelves.user_id = ".$_POST['uid']."

Couple things… For some odd reason shelf label is not showing up, but if I remove all other parts in the query, like book labels, it works. Also, I can only display in one long column (via loop). How would I break it into separate coulmns, or if in one column, how do I show shelf label only once per group of books?

  • 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-06T19:20:37+00:00Added an answer on June 6, 2026 at 7:20 pm

    1st: escaping the input vars

    2nd: remove LEFT in join, if you start from cross table leftjoin is nonsense (IMHO)

    3rd: use GROUP_CONCAT to return a string of set of titles, comma separated, 1 row per shelf

    $pid = mysql_real_escape_string( $_POST['pid'] );
    $uid = mysql_real_escape_string( $_POST['uid'] );
    
    $sql = "SELECT 
            shelf.label,
            GROUP_CONCAT( books.label separator ',') as titlelist
        FROM booksOnShelves
        JOIN shelf ON (shelf.id = booksOnShelves.shelf_id)
        JOIN books ON (books.id = booksOnShelves.book_id) 
        WHERE booksOnShelves.project_id = ".$pid."
        AND booksOnShelves.user_id = ".$uid."
    group by booksOnShelves.shelf_id";
    

    Of course your PHP has to parse/manage titlelist to fill the page

    ADD

    You can change the separator as your needs (eg: |) then

    foreach($results as $row){
    
    echo "<div class='float_left'><p class='bold'>".$row['label']."</p>\n";
    // using '|' as separator less common then ',' in the titles
    $array_of_titles = explode('|',$row['titlelist']);
    foreach($array_of_titles as $title){
    echo "<p>".$title."</p>\n";
    
    }
    echo "</div>\n";
    }
    

    I hypothesized $results is an associative array with all rows of your query.

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

Sidebar

Related Questions

I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two MySQL tables, j and t, with a third normalising table, jt.
I have this mysql tables I want to display with jqgrid. The problem appears
I have a complex MySQL query that joins three tables and self-joins one table
I have two MySQL tables structed as follows; advert +-------------------+--------------+ | Field | Type
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever
I have 2 mysql tables : Question with the following columns : id, question,
I have two MySQL tables, and I want to find and replace text strings
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,

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.