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

  • Home
  • SEARCH
  • 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 8657697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:35:10+00:00 2026-06-12T15:35:10+00:00

Question is similar to this one How to write a MySQL query that returns

  • 0

Question is similar to this one How to write a MySQL query that returns a temporary column containing flags for whether or not an item related to that row exists in another table

Except that I need to be more specific about which rows exists

I have two tables: ‘competitions’ and ‘competition_entries’

Competitions:

ID    | NAME      | TYPE
--------------------------------
1     | Example   | example type
2     | Another   | example type

Competition Entries

ID    | USERID    | COMPETITIONID
---------------------------------
1     | 100       | 1
2     | 110       | 1
3     | 110       | 2
4     | 120       | 1

I want to select the competitions but add an additional column which specifies whether the user has entered the competition or not. This is my current SELECT statement

SELECT
    c.[ID],
    c.[NAME],
    c.[TYPE],
    (CASE 
        WHEN e.ID IS NOT NULL AND e.USERID = @userid THEN 1 
        ELSE 0 
        END
    ) AS 'ENTERED'      

FROM competitions AS c
LEFT OUTER JOIN competition_entries AS e
ON e.COMPETITIONID = c.ID

My desired result set from setting the @userid parameter to 110 is this

ID    | NAME    | TYPE         | ENTERED
-------------------------------------
1     | Example | example type | 1
2     | Another | example type | 1

But instead I get this

ID    | NAME    | TYPE         | ENTERED
-------------------------------------
1     | Example | example type | 0
1     | Example | example type | 1
1     | Example | example type | 0
2     | Another | example type | 1  

Because it’s counting the entries for all user ids

  • 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-12T15:35:11+00:00Added an answer on June 12, 2026 at 3:35 pm

    Fixing your query

    SELECT
        c.[ID],
        c.[NAME],
        c.[TYPE],
        MAX(CASE 
            WHEN e.ID IS NOT NULL AND e.USERID = @userid THEN 1 
            ELSE 0 
            END
        ) AS 'ENTERED'      
    FROM competitions AS c
    LEFT OUTER JOIN competition_entries AS e ON e.COMPETITIONID = c.ID
    GROUP BY
        c.[ID],
        c.[NAME],
        c.[TYPE]
    

    An alternative is to rewrite it using EXISTS which is pretty much the same but may be easier to understand.
    BTW, using single quotes on the column name is deprecated. Use square brackets.

    SELECT
        c.[ID],
        c.[NAME],
        c.[TYPE],
        CASE WHEN EXISTS (
            SELECT *
            FROM competition_entries AS e
            WHERE e.COMPETITIONID = c.ID
              AND e.USERID = @userid) THEN 1 ELSE 0 END [ENTERED]
    FROM competitions AS c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is similar to this question. However, this one is about exceptions, not
This question is similar to this other one , with the difference that the
My question is similar to this one: Write a password protected Zip file in
Preamble: My core question is very similar to this one: How can I write
There is similar question to this one here but I guess that by this
I'm looking for an answer to a question similar to this one: protect users'
my question is similar to this one , but I don't want to use
This question is similar to this one, but with an extra wrinkle: Auto-removing all
This question is similar to this one How do I add options to a
My question is similar to this one , but I would like to replicate

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.