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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:04:39+00:00 2026-05-19T10:04:39+00:00

I am hoping for some help making my queries more efficient if possible. I

  • 0

I am hoping for some help making my queries more efficient if possible. I have two tables. One which contains the types of content available and another which contains content linked to the type table by a type id.

I am trying to select from the content table and group the results by type_id. I am currently using a query inside a loop to do this so it is selecting again from the second table for each type_id. Is there a better/more efficient way to do this?

Here are my current tables and queries:

type_id     type
  1        type 1
  2        type 2

id    title   type_id    content 
1      test1    1      test content 1
2      test2    2      test content 2
3      test3    1      test content 3



$query="select type_id, type from type_table";

foreach($query as $type){

$query="select title, content from content_table WHERE type_id=$type['type_id']";
}

I hope this makes sense.

Thanks

  • 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-19T10:04:39+00:00Added an answer on May 19, 2026 at 10:04 am

    Your example appears to select all possible values from the content table, albeit in batches of a given type at a time. So select title, content from content_table would surely do this whole thing in one go, without looping.

    To put each type_id into a new div, you could do something like this:

    SELECT title, content, t.type_id, t.type
    FROM content_table c
    JOIN type_table t ON t.type_id = c.type_id
    ORDER BY t.type_id
    
    // pseudo-code
    prev_type_id = -1
    print "<div>"
    foreach row:
        if type_id != prev_type_id:
            prev_type_id = type_id
            print "</div><div>"
    print "</div>"
    

    (edited based on comments)

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

Sidebar

Related Questions

I'm hoping people have some ideas to help solve this problem. I am developing
Hi guys I was hoping from some help here, please. I have a INSERT
I was hoping for some help, opposed to full solutions and I have an
I'm hoping someone on this site can offer some help. I have a page
I have a problem with my code and I was hoping for some help.
I am having troubles with jQuery's load function and am hoping for some help.
Hoping for some help after reading into MVC routing and not coming up with
I was hoping for some help to understand why I need to re-cast my
I have a general understanding of restrict but I'm hoping to clarify some fine
Hey guys, hoping for some help with this :( been stuck on it 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.