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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:10:37+00:00 2026-06-18T05:10:37+00:00

** Edit ** I used the query from the first answer, but eventually had

  • 0

** Edit **

I used the query from the first answer, but eventually had to modify it because It wouldn’t return more than one survey without any sessions. I finally worked out this query:

(SELECT i.id AS id, i.title AS title, c.title AS ctitle, c.color AS ccolor,
                     i.active AS active, i.length AS length, i.redirect AS redirect,
                     COUNT(s.sid) AS number
                FROM survey_info i
                LEFT JOIN survey_sessions s ON s.sid = i.id
                INNER JOIN survey_category c ON i.category = c.id
                WHERE s.sid IS NOT NULL
                GROUP BY s.sid)
                UNION ALL
                (SELECT i.id AS id, i.title AS title, c.title AS ctitle, c.color AS ccolor,
                       i.active AS active, i.length AS length, i.redirect AS redirect,
                       0 AS number
                FROM survey_info i
                LEFT JOIN survey_sessions s ON s.sid = i.id
                INNER JOIN survey_category c ON i.category = c.id
                WHERE s.sid IS NULL)
                ORDER BY 2

Which, to explain, Takes the query from below, and returns ONLY surveys that have sessions (have been taken) and then uses a UNION ALL clause to combine those results with a SELECT that retrieves ONLY surveys that have no sessions (have not been taken.) And finally, it orders the combined results by the second column (survey title)

I hope that this helps someone else out.

——————————————————————————————–

I’ve been working on a survey program that lets people create and administer surveys. Yesterday I had asked a question about storing results and realized that I had been doing it all wrong (create additional tables for each survey shudder)

I have a new table structure now that works much better and keeps me at 6 tables at all times.
I have a new dilemma that I’ve worked on all night, and hope someone can point me in the right direction.

My tables are now as follows:

survey_info

id(bigint)        PK
title(varchar)
category(bigint)
length(bigint)
active(tinyint)

survey_category

id(bigint)        PK
title(varchar)
color(varchar)

survey_sessions

id                PK
timestamp(bigint)

I was originally using the following SQL to gather the surveys and their number of sessions(times taken)

SELECT i.id AS id, i.title AS title, c.title AS ctitle, c.color AS ccolor,
       i.active AS active, i.length AS length, i.redirect AS redirect,
       COUNT(s.sid) AS number
FROM survey_info i, survey_category c, survey_sessions s
WHERE i.category = c.id AND s.sid = i.id
GROUP BY s.sid

However, as you may have guessed, this means that whenever I create a new survey (that has never been taken) it won’t show up in this list, because there are no entries in the survey_session table for it.

Is it possible to use the conditional statements in MySQL to return a 0 in place of “COUNT(s.sid) AS number” when there are no rows in survey_sessions? I had read a lot about LEFT JOIN’s but I didn’t think that they would work since there won’t be anything to connect the new survey to the survey_sessions 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-18T05:10:38+00:00Added an answer on June 18, 2026 at 5:10 am

    Here is your query rewritten to LEFT JOIN onto survey_sessions. You should be able to check easily enough if it works for you.

    SELECT i.id AS id, i.title AS title, c.title AS ctitle, c.color AS ccolor,
        i.active AS active, i.length AS length, i.redirect AS redirect,
        COUNT(s.sid) AS number
    FROM survey_info i
    INNER JOIN survey_category c ON i.category = c.id
    LEFT JOIN survey_sessions s ON s.sid = i.id
    GROUP BY s.sid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

edit: A more pointed question: What is the derivative of softmax to be used
The title speaks for itself. The first query - SELECT * FROM table _t
I'm using the following mysql query to return some data from my database. Currently
This query is running very slow; it used to be 3 separate queries but
I used the following query to find duplicates: SELECT userID, COUNT(userID) AS NumOccurrences FROM
EDIT: I used, finally, inotify. As stefanB says, inotify is the thing to use.
I used to edit Web.config before in order to allow some users to access
[edit] So I used one of the javascript tooltips suggested below. I got the
I was making a VB.NET application that can be used to edit, compile and
These days I'm working on a VB.NET application which can be used to edit,

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.