Pergunta

I´m having trouble creating a particular SQL query, in one query only (I can't go two times to the database, for design architecture, trust me on this) here are the statements:

I have four tables: Questions, Locations, Countries, Regions

This are some of their fields:

Questions
    id
    description
Locations
    id
    type (could be 'country' or 'region')
    question_id
    country_or_region_id (an id, that holds either the country or the region id)
Countries
    id
    name
Regions
    id
    name

What I want to get is this:

Example:

1       What is your name?        Venezuela, Colombia              South America

Format:

question id,      question description,       countries,        regions

Edit: For those who ask, I'm using MySQL

Edit: For those who say it is a bad design: I didn't create it, and I can't change the design, I just have to do it, as it is now.

Foi útil?

Solução

If this is MySQL:

SELECT  q.ID,
        q.Description,
        GROUP_CONCAT(DISTINCT c.name) AS countries,
        GROUP_CONCAT(DISTINCT r.name) AS regions
FROM    Questions q
        INNER JOIN Locations l
            ON l.question_id = q.id
        LEFT JOIN Countries c
            ON c.id = country_or_region_id
            AND l.type = 'country'
        LEFT JOIN Regions R
            ON R.id = country_or_region_id
            AND l.type = 'region'
GROUP BY q.ID, q.Description;

If this is SQL- Server:

SELECT  q.ID,
        q.Description,
        countries = STUFF(( SELECT ', ' + c.name
                            FROM    Locations l
                                    INNER JOIN Countries c
                                        ON c.id = country_or_region_id
                                        AND l.type = 'country'
                            WHERE   l.question_id = q.id
                            FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''),
        regions = STUFF((   SELECT ', ' + r.name
                            FROM    Locations l
                                    INNER JOIN Regions r
                                        ON r.id = country_or_region_id
                                        AND l.type = 'region'
                            WHERE   l.question_id = q.id
                            FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM    Questions q;

Outras dicas

Remove the Locations table and add question_id to the Regions and Countries table

select Q.id, Q.description, C.name as country, R.name as region 
from Questions as Q join Countries as C join Regions as R 
where Q.id = L.question_id and Q.id = C.question_id and Q.id = R.question_id;
SELECT questions.id
     , questions.description
     , Group_Concat(countries.name) As countries
     , Group_Concat(regions.name) As regions
FROM   questions
 INNER
  JOIN locations
    ON locations.question_id = questions.id
 LEFT
  JOIN countries
    ON countries.id = locations.country_or_region_id
   AND locations.type = 'country'
 LEFT
  JOIN regions
    ON regions.id = locations.country_or_region_id
   AND locations.type = 'region'
GROUP
    BY questions.id
     , questions.description
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top