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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:46:40+00:00 2026-06-14T10:46:40+00:00

I am new to sql and I really tried to solve this problem on

  • 0

I am new to sql and I really tried to solve this problem on my own, but no success…
Hope someone can help.

I have 3 tables:

Auth_user

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| username     | varchar(30)  | NO   | UNI | NULL    |                |
| first_name   | varchar(30)  | NO   |     | NULL    |                |
| last_name    | varchar(30)  | NO   |     | NULL    |                |
| email        | varchar(75)  | NO   |     | NULL    |                |
| password     | varchar(128) | NO   |     | NULL    |                |
| is_staff     | tinyint(1)   | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | NULL    |                |
| is_superuser | tinyint(1)   | NO   |     | NULL    |                |
| last_login   | datetime     | NO   |     | NULL    |                |
| date_joined  | datetime     | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

catalog_presents

+-------------+----------------------+------+-----+---------+----------------+
| Field       | Type                 | Null | Key | Default | Extra          |
+-------------+----------------------+------+-----+---------+----------------+
| id          | int(11)              | NO   | PRI | NULL    | auto_increment |
| name        | varchar(48)          | NO   |     | NULL    |                |
| slug        | varchar(50)          | NO   | UNI | NULL    |                |
| amount      | smallint(5) unsigned | NO   |     | 0       |                |
| points      | smallint(5) unsigned | NO   |     | 500     |                |
| created_at  | datetime             | NO   |     | NULL    |                |
| updated_at  | datetime             | NO   |     | NULL    |                |
| active      | tinyint(1)           | NO   |     | NULL    |                |
| image       | varchar(100)         | YES  |     | NULL    |                |
| description | longtext             | YES  |     | NULL    |                |
+-------------+----------------------+------+-----+---------+----------------+

catalog_orders

+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| id         | int(11)              | NO   | PRI | NULL    | auto_increment |
| user_id    | int(11)              | NO   | MUL | NULL    |                |
| status     | smallint(5) unsigned | NO   |     | 0       |                |
| present_id | int(11)              | NO   | MUL | NULL    |                |
| address_id | int(11)              | NO   | MUL | NULL    |                |
| created_at | datetime             | NO   |     | NULL    |                |
| updated_at | datetime             | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+

I am trying to select a user and check whether he ordered a particular present and write to the row accordingly, then check another one, and another one and so on.

Basically I would like the results to look like this:

id present1 present2 present3 present4
1 1 0 0
2 1 1 1
3 0 0 0
4 1 0 1

where 1 is user placed an order on that type of present and 0 is he didn’t.

My query looks like this

select auth_user.id, case when present_id = 1 from auth_user 
left join catalog_orders on catalog_orders.user_id = auth_user.id 
left join catalog_presents on catalog_presents.id = catalog_orders.present_id

The problem is that all different orders by users are sorted in multiple rows like this:

id present1 present2 present3 present4
1 1 0 0
2 1 0 0
2 0 1 0
2 0 0 1
3 0 0 0
4 1 0 0
4 0 0 1

Can somebody please help me solve this? 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-06-14T10:46:41+00:00Added an answer on June 14, 2026 at 10:46 am

    This is a kind of pivot table.

    You are on the right path, but you need to combine your results down to a single row per user.

    Use an aggregate MAX() per auth_user.id to collapse them into a single row. The CASE supplies a zero or one for each present, and the MAX() then selects the greatest value of all rows for the auth_user.id, which will be 1 if the present was bought and 0 otherwise.

    SELECT
      auth_user.id,
      MAX(CASE WHEN present_id = 1 THEN 1 ELSE 0 END) AS present1,
      MAX(CASE WHEN present_id = 2 THEN 1 ELSE 0 END) AS present2,
      MAX(CASE WHEN present_id = 3 THEN 1 ELSE 0 END) AS present3,
      MAX(CASE WHEN present_id = 4 THEN 1 ELSE 0 END) AS present4
    FROM
       auth_user
       LEFT JOIN catalog_orders on catalog_orders.user_id = auth_user.id 
       LEFT JOIN catalog_presents on catalog_presents.id = catalog_orders.present_id
    GROUP BY auth_user.id
    ORDER BY auth_user.id
    

    Specifically for MySQL, you don’t need the CASE since the boolean comparison present_id = 1 will return a 1 or 0 on its own. This is not portable to all other RDBMS though. The method above is preferred.

    SELECT
      auth_user.id,
      /* MySQL ok, not all other RDBMS will do this - boolean comparison returns 1 or 0 */
      MAX(present_id = 1) AS present1,
      MAX(present_id = 2) AS present2,
      MAX(present_id = 3) AS present3,
      MAX(present_id = 4) AS present4
    FROM
       auth_user
       LEFT JOIN catalog_orders on catalog_orders.user_id = auth_user.id 
       LEFT JOIN catalog_presents on catalog_presents.id = catalog_orders.present_id
    GROUP BY auth_user.id
    ORDER BY auth_user.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to SQL Server and would really appreciate some help with this.
im really new to linq-to-SQL so this may sound like a really dumb question,
I'm new to SQL. I have a simple problem with getting the results from
We have a security problem here and we really need your help and inputs.
this is probably a really stupid problem, but i've been looking at it for
I have a really simple question here, but I just can't find an answer
I've got a strange problem regarding linq-to-sql, and i've really tried search around for
I`m new to sql and have been stuck on the following issue for almost
I'm new to SQL and I am having trouble even starting this query. If
I've searched high and low for the answer to this, but I can't figure

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.