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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:07:36+00:00 2026-06-17T02:07:36+00:00

I have two tables, a main one, and one that supports the main table,

  • 0

I have two tables, a main one, and one that supports the main table, very very similar to what wordpress has, posts and posts_meta.

Main table:

id
title,
content

id  | title     | content
1   | one       | content one
2   | two       | content two

Meta table:

id
item_id
key
value

id  | item_id   | key       | value
1   | 1         | template  | single
2   | 1         | group     | top
1   | 2         | template  | page
2   | 2         | group     | bottom

And my goal is, in the end, have an array with the data from the main table, merged with the meta table. example:

$data = array(
    array(
        'id' => 1,
        'title' => 'one',
        'content' => 'content one',
        'template' => 'single',
        'group' => 'top'
    ),
    array(
        'id' => 2,
        'title' => 'two',
        'content' => 'content two',
        'template' => 'page',
        'group' => 'bottom'
    )
);

What is the best way to achieve this in a way that preforms good?

I am using PDO to connect to my database, and how Im doing right now is, I first query the data on the first table, and then for each result, i query the meta table, I use prepared statements for this, since it’s suposed to be fast, but even so, it’s harming the performance of my script.

Thank you

  • 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-17T02:07:38+00:00Added an answer on June 17, 2026 at 2:07 am

    Instead of querying meta table for each result from first query

    you should extract the ids from the first result:

    $rows = q('SELECT * FROM posts');
    $byIds = [];
    foreach ($rows as &$row)
    {
        $byIds[$row['id']] =& $row;
    }
    

    and run second query:

    $rows2 = q('SELECT * FROM posts_meta WHERE item_id IN (' . implode(',', array_keys($byIds)) . ')');
    

    Then loop the results in PHP and merge with first query results.

    foreach ($rows2 as $row2)
    {
        $byIds[$row2['item_id']][$row2['key']] = $row2['value'];
    }
    

    You have your merged results in $rows variable now:

    var_dump($rows);
    

    This way you will have only 2 db requests.

    Please note that i have used $byIds as array of references so i dont have to search row with specific id in second loop. This way order of elements in $rows are preserved.

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

Sidebar

Related Questions

I have two tables that are something like this: Main table: id (int), title
In SQL Server, I have two tables. One main table with all the data
I have two tables. Table1 is our main table of data and Table2 has
I have a question regarding the design of two tables. Table 1: The main
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables in APEX that are linked by their primary key. One
I have two tables on mysql: users, and management. The users table has a
I have two tables (country & ducks) where the country table has every country
I have two tables with hierarchyid fields, one of which is a staging table
Ok I have two tables. Table IDAssoc has the columns bill_id , year ,

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.