简体   繁体   中英

During SELECT determine number of related rows (0 - n) in another table

** 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM