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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:13:43+00:00 2026-06-10T21:13:43+00:00

I did my homework and looked for information on the net and in StackOverflow

  • 0

I did my homework and looked for information on the net and in StackOverflow but I did not find an answer to my question. I looked at:

Creating two colums from rows of a table (did not understand it)
MySQL join same table display rows in columns way (complicated) (different and problem does not seem the same)
Creating two colums from rows of a table

I used this to build my query:

How to Display rows as Columns in MySQL?

I have a table “lessons” that goes like this:

ID  title
---------
1   Math
2   Latin
3   English
4   Science 

I have a second table “results” that stores the type of test user have passed for each lesson. There are currently two types of tests: o and w (there could be more in the future). The table goes like this:

lesson_id  usr_id  test_type   course_id
----------------------------------------
1          100       o          1
1          100       w          1
1          24        o          1
1          36        w          1

In the table above user 100 passed test o and w for the Math.
In the table above user 24 passed test o for the Math.
In the table above user 36 passed test w for the Math.

Now I would like to get a report for user 100, I would like to have something like:

ID  title      o       w
------------------------------
1    Math       TRUE    TRUE
2    Latin      FALSE   FALSE
3    English    FALSE   FALSE
4    Science    FALSE   FALSE

For user 36:

ID  title      o       w
------------------------------
1    Math       FALSE    TRUE
2    Latin      FALSE   FALSE
...

For user 24:

ID  title      o       w
------------------------------
1    Math       TRUE    FALSE
2    Latin      FALSE   FALSE
...

Any other user:

ID  title      o       w
------------------------------
1    Math       FALSE   FALSE
2    Latin      FALSE   FALSE
...

I can accomplish this with a query as follows:

SELECT l.id, l.title, COALESCE(s.o,FALSE) AS o, COALESCE(t.w, FALSE) AS w FROM lessons l 
LEFT JOIN (
    SELECT r.lesson_id,
    CASE
        WHEN r.test_type='o' THEN TRUE
        ELSE FALSE
    END AS o
    FROM results r WHERE r.itype='o' AND r.usr_id=100
    ) AS s ON l.id=s.lesson_id 
LEFT JOIN (
    SELECT rr.lesson_id,
    CASE
        WHEN rr.test_type='w' THEN TRUE
        ELSE FALSE
    END AS w 
    FROM results rr WHERE rr.test_type='w' AND rr.usr_id=100
    ) AS t ON l.id=t.lesson_id
WHERE l.course_id=1 ORDER BY l.id ASC;

While this code seems to work (still busy testing it), I do not like it because it requires two joins and each join is made of selects from a table that could turn out to be quite large in the future. This query would be run often

  1. Can you suggest a better way to do this (better can mean smarter, more straightforward, faster or other design… or any other suggestion 🙂 )?

  2. In case I need to stick with this, could you recommend references as how to set the best indexes to run this query fast? Links, articles you used?

  3. What happens where more test_types will be created? My query uses only w and o what if tomorrow there are more? Is there a generic way to do this kind of thing?

Any help / advice is more than welcome,

Philippe

  • 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-10T21:13:45+00:00Added an answer on June 10, 2026 at 9:13 pm

    I see this as a simple aggregation query. The aggregation is by lesson_id and title. The calculation then just pivots the results for o and w:

    select l.lesson_id, l.title,
           max(case when r.test_type = 'o' then 'TRUE' else 'FALSE' end) as o,
           max(case when r.test_type = 'w' then 'TRUE' else 'FALSE' end) as w
    from lessons l left outer join
         results r
         on l.lesson_id = r.lesson_id
    where r.user_id = <whatever>
    group by l.lesson_id, l.title
    order by 1
    

    Choosing by the user at this level is a problem because it needs to come from results, which may be NULL due to the left outer join. Here is a variation that fixes the problem:

    select l.lesson_id, l.title,
           max(case when r.test_type = 'o' then 'TRUE' else 'FALSE' end) as o,
           max(case when r.test_type = 'w' then 'TRUE' else 'FALSE' end) as w
    from lessons l left outer join
         results r
         on l.lesson_id = r.lesson_id and
            r.user_id = <whatever>
    group by l.lesson_id, l.title
    order by 1
    

    By moving the condition to the “on” clause, we are guaranteed that all lessons will be kept.

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

Sidebar

Related Questions

I did quite a bit of homework before posting here but still could not
This is not a homework, it's more of a challenge from our prof but
Please note that this is not homework and i did search before starting this
Did some searches here & on the 'net and haven't found a good answer
Did you ever have the following situation: you need to store information, but a
While this may look like homework, I assure you it's not. It stems from
This IS NOT a Homework question! While building my current student database project I
Little problem about sending PHP array to javascript function, i did homework looked everywhere
Yeah, I did my homework and I found the setFrame:frame display:YES animate:YES but I
I went through all the questions on SOF and did my homework but still

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.