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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:23:12+00:00 2026-06-02T08:23:12+00:00

I am looking for an SQL query that will return a 1 or a

  • 0

I am looking for an SQL query that will return a 1 or a zero depending on a joined row’s content.

Two Tables:

Events:

  • event_id
  • descriptors

Qualifiers:

  • qualifier_id
  • event_id
  • qualifier_type
  • value

Each event can have multiple qualifiers.

The query that I currently have is:

select event_id, if(qualifier_type = 15, 1, 0)
from events 
join qualifiers q using (event_id)
where q.qualifier_type = 15;

In a perfect world, I would nicely normalize these tables and put the different qualifiers in with the events, but this isn’t possible at the moment.

Edit: Currently, this query only returns rows that have the association. Instead, I need to return all rows and an extra column specifying if this association exists.

  • 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-02T08:23:13+00:00Added an answer on June 2, 2026 at 8:23 am
    SELECT e.event_id, COUNT(q.qualifier_type) AS needs_qualtype_15
      FROM events AS e
      LEFT JOIN qualifiers AS q ON q.event_id = e.event_id AND q.qualifier_type = 15
     GROUP BY e.event_id
    

    This gives you a list of all events with an indication of whether the qualifier type 15 is associated with the event.

    Another way of writing the same thing is:

    SELECT e.event_id, COUNT(q.qualifier_type) AS needs_qualtype_15
      FROM events AS e
      LEFT JOIN
           (SELECT event_id, qualifier_type
              FROM qualifiers WHERE qualifier_type = 15
           ) AS q
        ON q.event_id = e.event_id
     GROUP BY e.event_id;
    

    Both formulations rely on COUNT(expr) only counting non-null values. For the events with a matching qualifier, the COUNT is 1; for the events without, it is 0. The first version compresses the query by including the condition on qualifier type into the ON join condition. The second version is clearer, but more verbose. The chances are that a good optimizer will execute both queries the same way.

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

Sidebar

Related Questions

I'm looking for a SQL Server 2005 query that will list all the indexes
SQL Server 2005. Is there a sql query that will return a text field
I am looking to create a SQL query that collects the results from multiple
i am looking for an SQL query, that selects exactly rows ordered by date
I'm looking for a way to query a SQL database with Java and return
I'm looking to create a function that generates an SQL query, filtering the results
Basically I'm looking to create a page using PHP that will take SQL input,
I need a SQL query that will identify seasonal sales items. My table has
I have a query I want to run as a subquery that will return
I'm looking for help using sum() in my SQL query: SELECT links.id, count(DISTINCT stats.id)

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.