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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:51:05+00:00 2026-05-13T01:51:05+00:00

I have 3 tables in my MySql database. ————- | countries | ————- |

  • 0

I have 3 tables in my MySql database.

-------------
| countries |
-------------
| iso       |
| name      |
-------------

--------------------
| documents        |
--------------------
| id               |
| country_iso      |
| publication_date |
--------------------

---------------
| files       |
---------------
| document_id |
| name        |
---------------

Could you suggest a mysql query and php function to print this to a table as below.
Where d = documents->files->name, each year can have many document for a particular country.

-----------------------------------------
|           | 2009 | 2008 | 2007 | 2006 |
-----------------------------------------
| country a | d d  |      | d    | d    |
| country b |      | d    | d    |      |
| country c | d    | d d  |      | d    |
| country d | d    |      | d d  |      |
-----------------------------------------

Thanks in advance

  • 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-13T01:51:05+00:00Added an answer on May 13, 2026 at 1:51 am

    If there can be only one file per each document, you should combine the documents and files table so that the documents table has a filename field.

    This example assumes one-to-one relation between files and documents. Instead of creating exotic SQL-queries, it’s simpler to just select plain data from DB and transform it using PHP.

    SELECT
        countries.name country,
        documents.name document,
        files.name filename,
        DATE_FORMAT(publication_date, '%Y')
    FROM 
        documents
    LEFT JOIN
        countries
    ON
        documents.country_iso = countries.iso
    LEFT JOIN 
        files 
    ON 
        documents.id = files.document_id
    

    That will result in a followin table from the database:

    country    | document     | filename     | year
    ----------------------------------------------------
    Germany    | doc_1        | doc_1.pdf    | 2009
    Germany    | doc_2        | doc_2.pdf    | 2007
    Germany    | doc_3        | doc_3.pdf    | 2007
    Norway     | doc_6        | doc_6.pdf    | 2008
    ...
    

    Next that table has to be transformed with PHP to proper form. The proper form in this case would be that all documents are indexed by year which are in turn indexed by customer:

    $q = mysql_query("SELECT ...");
    $result = array();
    $years = array();
    
    while($row = mysql_fetch_assoc($q)) {
        $result[$row['country']][$row['year']][] = array('document' =>  $row['document'], 'filename' => $row['filename']);
        $years[] = $row['year'];
    }
    
    // Max and min years are used to print the table header
    $max_year = max($years);
    $min_year = min($years);
    

    Now, if you would like to find Germany’s documents for year 2009, you could just use $result['Germany'][2009];. But you can use a nifty looping to print the table automatically:

    <table>
        <tr>
            <th>Country</th>
            <?php for($y = $max_year; $y >= $min_year; $y--): ?>
            <th><?php echo $y; ?></th>
            <?php endfor; ?>
        </tr>
        <?php foreach($result as $country => $years): ?>
        <tr>
            <td><?php echo $country; ?></td>
            <?php for($y = $max_year; $y >= $min_year; $y--): ?>
            <td>
                <?php if(isset($years[$y])): foreach($years[$y] as $document): ?>
                    <a href="<?php echo $document['filename']; ?>">
                        <?php echo $document['name']; ?>
                    </a>                
                <?php endforeach; endif; ?>
            </td>
            <?php endfor; ?>
        </tr>
        <?php endforeach; ?>
    </table>
    

    There you go. Note that I haven’t tested this, there might be parse errors and some other illogicalities but I guess you get the idea from this.

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

Sidebar

Related Questions

I have a MySql database with three tables I need to combine in a
I have a mysql database which has grown to over 200 tables in it.
I have the following tables in MySQL: team: id, name, [more stuff] person: id,
I have a user table in my mysql database that has a password column.
I have a MySQL database table with a couple thousand rows. The table is
I have two mysql tables, one containing details on cars and one containing all
I have a question about tables in MySQL. I'm currently making a website where
I have a MySQL Left Join problem. I have three tables which I'm trying
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a database for an E-commerce storefront. MSSQL 2008. I have a table

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.