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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:30:01+00:00 2026-05-20T13:30:01+00:00

I have three tables: pages, blocks and pages_blocks. My database schema looks as follow:

  • 0

I have three tables: pages, blocks and pages_blocks.

My database schema looks as follow:

CREATE TABLE `blocks` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `name` char(40) NOT NULL,
  `content` text NOT NULL
  PRIMARY KEY (`id`)
);

CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` char(100) NOT NULL,
  `content` mediumtext NOT NULL
  PRIMARY KEY (`id`)
);

CREATE TABLE `pages_blocks` (
  `page_id` int(11) DEFAULT NULL,
  `block_id` smallint(6) DEFAULT NULL,
  `location` enum('left','right') DEFAULT NULL,
  `display_order` smallint(6) DEFAULT NULL
);

What would be the perfect SQL code to grab the “left” and “right” blocks for a specific page?

Your help is much appreciated.

  • 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-05-20T13:30:02+00:00Added an answer on May 20, 2026 at 1:30 pm

    One way would be two joins searching left and right:

    select  *
    from    pages p
    left join
            (
            select  *
            from    pages_blocks pb
            join    blocks b
            on      pb.block_id = b.id
            where   pb.location = 'left'
            ) left_block
    on      left_block.page_id = p.id
    left join
            (
            select  *
            from    pages_blocks pb
            join    blocks b
            on      pb.block_id = b.id
            where   pb.location = 'right'
            ) right_block
    on      right_block.page_id = p.id
    where   p.id = 42
    

    This has the advantage of returning only one row without using group by.

    EDIT: if there can be multiple “right” and “left” blocks, you could query all blocks like:

    select  pb.location
    ,       pb.display_order
    ,       b.name
    ,       b.content
    from    pages p
    join    pages_blocks pb
    on      pb.page_id = p.id
    join    blocks b
    on      pb.block_id = b.id
    where   p.id = 42
    order by
            pb.location
    ,       pb.display_order
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables - 1-FriendShip, 2-Users, 3-Pages. Friendship table has fromUser, and toUser
I have three tables of data: table: cars [10,000 rows] table: planes [2,000 rows]
I have three tables in a Mysql database - countries, cities and hotels. Their
I need to create module in Magento which will have few database tables. One
I have three tables. I have to retrieve the data using Linq statement. My
I have three tables (individuals, groups & recordlabels) and want use the information between
I have three tables: Employee(EmployeeID,Fname,Lname...) ProjectHeader(ProjectID,LeadID,Status....) ProjectDetails(ProjectDetailsID,ProjectID....) Here's my current code: $get_projects = SELECT
I have three tables as shown in below image. Note: Lead column of projectheader
I was looking on SO about create tables only if they do not exist
I have a web-page that looks like this (simplified example) <table id=events_results_table> <thead> <tr>

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.