0

私はこのテーブルを持っています:

idSection   | idQuestion    | title     | enunciation | idScale
1           | 1             | title 1   | question 1  |    3      
1           | 1             | title 1   | question 1  |    3      
1           | 1             | title 1   | question 1  |    3      
1           | 1             | title 1   | question 1  |    2
1           | 1             | title 1   | question 1  |    5
1           | 2             | title 2   | question 2  |    1      
1           | 2             | title 2   | question 2  |    3      
1           | 3             | title 3   | question 3  |    1      

そして、このテーブルを持っています:

idScale   |       name 
   1      |      Very Bad
   2      |         Bad
   3      |         Good
   4      |      Very Good
   5      |      Excellent

私はこのようなテーブルが欲しかった:

idSection   | idQuestion    | title     | enunciation | Total  | Name
1           | 1             | title 1   | question 1  |    0   |  Very Bad
1           | 1             | title 1   | question 1  |    0   |  Bad
1           | 1             | title 1   | question 1  |    3   |  Good
1           | 1             | title 1   | question 1  |    0   |  Very Good
1           | 1             | title 1   | question 1  |    1   |  Excellent
1           | 2             | title 2   | question 2  |    0   |  Very Bad
1           | 2             | title 2   | question 2  |    1   |   Bad
1           | 2             | title 2   | question 2  |    3   |  Good 
1           | 2             | title 2   | question 2  |    0   |  Very Good
1           | 2             | title 2   | question 2  |    0   |  Excellent

クエリ:

SELECT 
    t1.idSection, t1.idQuestion, t1.title, t1.enunciation, 
    COUNT(t1.idScale) as Total, t2.name 
FROM 
    table1 AS t1
INNER JOIN 
    table2 as t2 ON t2.idScale = t1.idScale
GROUP BY 
    t1.idSection, t1.idQuestion, t1.title, t1.enunciation, t2.name

この結果はクエリです。

idSection   | idQuestion    | title     | enunciation | Total  | Name
1           | 1             | title 1   | question 1  |    3   |  Good
1           | 1             | title 1   | question 1  |    1   |  Excellent
1           | 2             | title 2   | question 2  |    1   |   Bad
1           | 2             | title 2   | question 2  |    3   |  Good 

これの問題は、0 のクエリ値が表示されないことです。

4

3 に答える 3

3

私はあなたがこれを探していると思います:

SELECT
  t1.idSection,
  t1.idQuestion,
  t1.title,
  t1.enunciation, 
  SUM(case when t1.idScale=t2.idScale then 1 else 0 end) as Total,
  t2.name
FROM
  table1 AS t1, table2 as t2
GROUP BY t1.idSection, t1.idQuestion, t1.title, t1.enunciation, t2.name, t2.idScale
ORDER BY t1.idSection, t1.idQuestion, t2.idScale

これは INNER JOIN ではなく、代わりにデカルト結合です (table1 のすべての行が table2 のすべての行に対して乗算されます)。SUM を使用して、INNER JOIN が成功した行をカウントしています。

于 2013-01-14T11:56:43.487 に答える
1

これを試して:

(SELECT t1.idSection, t1.idQuestion, t1.title, t1.enunciation, 
   COUNT(t1.idScale) as Total, t2.name 
 FROM table1 t1
      JOIN table2 t2 
       ON t2.idScale=t1.idScale
GROUP BY t1.idSection, t1.idQuestion, t1.title, t1.enunciation, t2.name)
UNION 
 (SELECT DISTINCT 
  t1.idSection, idQuestion, title, enunciation, 0 as Total , t2.name
  FROM table1 t1,table2 t2
   WHERE NOT EXISTS
  (SELECT  *
   FROM table1 
        JOIN table2 
         ON table2.idScale=table1.idScale
   WHERE t1.idSection = table1.idSection
     AND t1.idQuestion = table1.idQuestion
     AND t2.idScale= table2.idScale)
)

http://sqlfiddle.com/#!3/7332c/15

于 2013-01-14T11:44:35.697 に答える
0

これを試して:

SELECT t1.idSection, t1.idQuestion, t1.title, t1.enunciation, 
COUNT(t1.idScale) as Total, t2.name FROM table1 AS t1
RIGHT JOIN table2 as t2 ON t2.idScale=t1.idScale
GROUP BY t1.idSection, t1.idQuestion, t1.title, t1.enunciation, t2.name
于 2013-01-14T12:02:13.247 に答える