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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:58:32+00:00 2026-05-27T18:58:32+00:00

I have joined multiple tables with INNER JOIN and got the output which resembles

  • 0

I have joined multiple tables with INNER JOIN and got the output which resembles this:

SPORT COUNTRY LEAGUE MATCH
SPORT COUNTRY LEAGUE MATCH
SPORT COUNTRY LEAGUE MATCH

I’d like SPORT to be displayed only the first time it’s found in the query. Same with COUNTRY and LEAGUE, so they serve as a header for the matches. Like this:

SPORT
COUNTRY
LEAGUE1
MATCH
MATCH
MATCH
LEAGUE2
MATCH
MATCH
MATCH

The code:

if ($db_found) {

$SQL ="
select sportname, 
  tournament_templatename, 
  tournament_stagename,    
  GROUP_CONCAT(eventname) as event_name
from ('SELECT 
         event.id AS eventid,
         event.name AS eventname,
         event.tournament_stageFK AS eventtournamentstageFK,
         event.startdate AS eventstartdate,
         tournament_stage.id AS tournament_stageid,
         tournament_stage.name AS tournament_stagename,
         tournament_stage.tournamentFK AS tournament_stagetournamentFK,
         tournament.id AS tournamentid,
         tournament.name AS tournamentname,
         tournament.tournament_templateFK AS tournamenttournament_templateFK,
         tournament_template.id AS tournamenttemplateid,
         tournament_template.name AS tournament_templatename,
         tournament_template.sportFK AS tournament_templatesportFK,
         sport.id AS sportid,
         sport.name AS sportname
       FROM event 
         INNER JOIN tournament_stage 
           ON event.tournament_stageFK=tournament_stage.id
         INNER JOIN tournament 
           ON tournament_stage.tournamentFK=tournament.id 
         INNER JOIN tournament_template 
           ON tournament.tournament_templateFK=tournament_template.id 
         INNER JOIN sport 
           ON tournament_template.sportFK=sport.id
       WHERE
         DATE(event.startdate) = CURRENT_DATE() 
       ORDER BY
         sport.id ASC, tournament_template.name ASC, event.startdate ASC'
     )
GROUP BY 
  sportname, tournament_templatename, tournament_stagename
ORDER BY 
  sportname, tournament_templatename, tournament_stagename";
$result = mysql_query($SQL);

echo "<table border='1' style='border-collapse: collapse;border-color: white;
font-size:8px; background-color: #c8ccce; width:100%;'>";

while($db_field=mysql_fetch_assoc($result)){
echo  "<tr>" , "<td>" , $db_field['sportname'] , "</td> " , "<td>" ,
$db_field['tournament_templatename'] , "</td>" , "<td>" , 
$db_field['tournament_stagename'] , "</td>" , "<td>" , $db_field['eventstartdate'] ,
"</td>" , "<td>" , $db_field['eventname'] , "</td>" , "</tr>";
}
echo "</table>";
mysql_close($db_handle);
}

Thanks for helping out a PHP newbie.

  • 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-27T18:58:33+00:00Added an answer on May 27, 2026 at 6:58 pm

    Over your query you can:

    $SQL ="
    select sportname, 
       tournament_templatename, 
       tournament_stagename,    
       GROUP_CONCAT(eventname) as eventname
    from (
    SELECT event.id AS eventid,
    event.name AS eventname,
    event.tournament_stageFK AS eventtournamentstageFK,
    event.startdate AS eventstartdate,
    tournament_stage.id AS tournament_stageid,
    tournament_stage.name AS tournament_stagename,
    tournament_stage.tournamentFK AS tournament_stagetournamentFK,
    tournament.id AS tournamentid,
    tournament.name AS tournamentname,
    tournament.tournament_templateFK AS tournamenttournament_templateFK,
    tournament_template.id AS tournamenttemplateid,
    tournament_template.name AS tournament_templatename,
    tournament_template.sportFK AS tournament_templatesportFK,
    sport.id AS sportid,
    sport.name AS sportname
    FROM
    event INNER JOIN tournament_stage ON event.tournament_stageFK=tournament_stage.id
    INNER JOIN tournament ON tournament_stage.tournamentFK=tournament.id 
    INNER JOIN tournament_template 
    ON tournament.tournament_templateFK=tournament_template.id 
    INNER JOIN sport ON tournament_template.sportFK=sport.id
    WHERE
    DATE(event.startdate) = CURRENT_DATE() 
    ) a
    group by sportname, tournament_templatename, tournament_stagename
    order by sportname, tournament_templatename, tournament_stagename";
    

    and then fetch the results, with first 3 columns already unique.

    php code remains the same:

    $result = mysql_query($SQL);
    
    while($db_field=mysql_fetch_assoc($result)){
    echo $db_field['sportname'] , " " , $db_field['tournament_templatename'] , " " ,      
    $db_field['tournament_stagename']  ,  " " , $db_field['eventname'];
    echo "<br>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem getting sorting to work with multiple joined tables. For example:
I currently have quite a messy query, which joins data from multiple tables involving
I have a few tables which I have joined together and would like to
I used the mapping solution from this question to have a joined component. But
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I am trying to pull multiple columns from 3 joined tables, but I want
I have joined three tables into a view, and then i try to update
I have 2 tables in MYSQL that will be joined by a foreign key.
Currently I have a database with about 10 tables. I have successfully joined them

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.