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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:58:28+00:00 2026-05-11T02:58:28+00:00

I have the following tables in MySQL: team: id, name, [more stuff] person: id,

  • 0

I have the following tables in MySQL:

team: id, name, [more stuff] person: id, name, team, [more stuff] entry: id, name, team, [more stuff] registrations: id, event_id, team, status registration_people: registration_id, person_id registration_entries: registration_id, entry_id 

I would like to query the database, and return details about each person and entry; grouped by registration. I’ve learned over the years to do as much as I can in SQL. However, I don’t see a good way to generate this in a single query without doing a lot of work in PHP. The naive way to do this would be [pseudocode]:

$registrations = $model->get_registrations($event_id); foreach ($registrations as $registration) {     $registration['people'] = $model->get_people_in_registration($registration['id']);     $registration['entries'] = $model->get_entries_in_registration($registration['id']); } return $registrations; 

This code does many round-trips to the server, and shouldn’t be used. The other idea I had was to do something of the like:

$registrations = $model->get_registrations($event_id); $registration_entries = $model->get_registration_entries($event_id); $registration_people = $model->get_registration_people($event_id); foreach ($registrations as $r)     $reg[$r['id']] = $r; foreach ($registrations_entries as $e)     $reg[$e['registration_id']]['entries'][] = $e; foreach ($registrations_people as $p)     $reg[$p['registration_id']]['people'][] = $p; return $reg; 

Where get_registration_entries/get_registration_people does a join, and grabs all registration_entries that are associated with the registrations in $event_id. However, this seems like I’m doing a join in PHP; when I might be able to get SQL to do it for me. Am I just being paranoid, or am I missing something here?

  • 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. 2026-05-11T02:58:29+00:00Added an answer on May 11, 2026 at 2:58 am

    The ‘do as much in SQL as possible’ concept is only valid when it decreases amount of data read from the database or the number of roundtrips to the database. If you need to get the data anyway, what’s the point of grouping it in the database? It is much easier to do in PHP.

    You should do something like this:

    select r.registration_id, p.* from registration_people rp, registrations r, person p where /* your filter conditions on the 'registration' table */ and rp.registration_id = r.id and p.id = rp.person_id 

    (I guess this is pretty much the same as your second approach)

    This way, you get all the relevant data from the database, and no more. Now you can group the results locally in PHP. This approach is quite efficient as you only get as much data from the database as you need and you don’t make unnecessary roundtrips to the database.

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Best practice would be to do this using IHierarchyData and… May 11, 2026 at 10:14 am
  • added an answer If all you want is a GUI, wxPython should do… May 11, 2026 at 10:14 am
  • added an answer You can index Dictionary, you didn't need 'get'. Dictionary<string,string> example… May 11, 2026 at 10:14 am

Related Questions

I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have the following table in MySQL (version 5): id int(10) UNSIGNED No auto_increment
I have the following tables in my database that have a many-to-many relationship, which
I have the following table in MSSQL2005 id | business_key | result 1 |
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following table and data in SQL Server 2005: create table LogEntries
In a table I have the following schema table1: playerID int primary key, nationalities
I have the following 3 rails classes, which are all stored in one table,
I have the following style in an external CSS file called first.css table {
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.