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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:17:07+00:00 2026-06-08T03:17:07+00:00

I have two mySQL tables in my database Training: +————-+————+————+————+-…-+————+ | Training_ID | 09-06-2012

  • 0

I have two mySQL tables in my database

Training:

+-------------+------------+------------+------------+-...-+------------+
| Training_ID | 09-06-2012 | 16-06-2012 | 23-06-2012 | ... | 28-12-2013 |
+-------------+------------+------------+------------+-...-+------------+
|     1       |    123     |    924     |   367      |     |   679      |
+-------------+------------+------------+------------+-...-+------------+
|     2       |    243     |    626     |   381      |     |   771      |
+-------------+------------+------------+------------+-...-+------------+
|     3       |    766     |    826     |   956      |     |   725      |
+-------------+------------+------------+------------+-...-+------------+

Users:

+---------+----------+
| User_ID | Name     |
+---------+----------+
|   123   | Fred     | 
+---------+----------+
|   924   | James    | 
+---------+----------+ 
|   367   | Tim      | 
+---------+----------+
|   766   | Mark     | 
+---------+----------+

What I want to show is:

+-------------+------------+-------------+------------+-...-+------------+
| Training_ID | 09-06-2012 | 16-06-2012  | 23-06-2012 | ... | 28-12-2013 |
+-------------+------------+-------------+------------+-...-+------------+
|     1       | 123 - Fred | 924 - James | 367 - Tim  |     | 679 - Ben  |
+-------------+------------+-------------+------------+-...-+------------+
|     2       | 243 - Sarah| 626 - Smith | 381 - Exam |     | 771 - John |
+-------------+------------+-------------+------------+-...-+------------+
|     3       | 766 - Mark | 826 - Jone  | 956 - Case |     | 725 - Brett|
+-------------+------------+-------------+------------+-...-+------------+

Note: there are actually 70 columns (all the same format as the above).

I tried using joins – but I cant do one for each column.

Is there a way to apply a join to the whole table?

  • 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-08T03:17:10+00:00Added an answer on June 8, 2026 at 3:17 am

    That looks like a terrible design and any query you’re going to perform to produce your desired format will also look equally as horrid, but it isn’t impossible:

    What you can do is make a single Cartesian-join of the training and users table, then GROUP BY the training_id, and make a bunch of conditional aggregations which will put everything on one row per training_id:

    SELECT
        a.training_id,
        MAX(CASE WHEN a.`09-06-2012` = b.user_id THEN CONCAT(a.`09-06-2012`, ' - ', b.name) END) AS `09-06-2012`,
        MAX(CASE WHEN a.`16-06-2012` = b.user_id THEN CONCAT(a.`16-06-2012`, ' - ', b.name) END) AS `16-06-2012`,
        MAX(CASE WHEN a.`23-06-2012` = b.user_id THEN CONCAT(a.`23-06-2012`, ' - ', b.name) END) AS `23-06-2012`,
        MAX(CASE WHEN a.`28-12-2013` = b.user_id THEN CONCAT(a.`28-12-2013`, ' - ', b.name) END) AS `28-12-2013`
    FROM training a
    CROSS JOIN users b
    GROUP BY a.training_id
    

    You’ll need as many MAX(CASE WHEN… columns in your SELECT clause as there are columns in your table.

    While this may also look ugly, it is much more efficient than joining the users table 70 different times for each column.


    SQLFiddle Demo with just four columns

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

Sidebar

Related Questions

I have two tables in my MySQL database, one is a library of all
I have two tables in a MySql database: COUNTRY --------------- id, country_name and CITY
In a MySQL database I have two tables linked in a join. One table
I am working on PHP and database MySQL. I have two tables in SQL
What I have is two tables inside of a mysql database. One table contains
I have the following two tables in my mysql database. Table Name: groups id
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables in a MySQL database, Locations and Tags, and a third
If I have two tables in a MySQL database that both have a column
I have two tables in a MySQL database, courses and sessions. I'm trying to

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.