【问题标题】:SQL query inner join with 0 valuesSQL查询内连接0值
【发布时间】:2012-12-28 08:23:20
【问题描述】:

我有这张桌子:

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的不会出现。

【问题讨论】:

  • idEscalaEntrada 字段从何而来?
  • @Bulat 抱歉复制粘贴问题...是 idScale...我编辑...
  • 您是否尝试过使用RIGHT JOIN 而不是INNER JOIN
  • 我什至会说 LEFT JOIN
  • 您的第二个表没有 0,因此 0 不会出现在选择中。添加 0 或使用外部(左、右)连接

标签: sql sql-server join inner-join


【解决方案1】:

我想你正在寻找这个:

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 成功的行数。

【讨论】:

  • @sh0rt6r 它工作正常,应该没问题,你得到哪个错误?我会做一个小提琴
  • @sh0rt6r 抱歉,我在某处读过 mysql... 已修复答案,请参阅此处 sqlfiddle.com/#!6/7332c/4/0
  • @fthiella 它被标记为 sas mysql...之前..很久没见队友 :)
  • @bonCodigo :) :) 很高兴再次见到你 :)
  • 是的,我放了 mysql 标签但改变了。我想这正是我想要的。
【解决方案2】:

试试这个:

(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

【讨论】:

  • 你的查询结果和我的一样。
  • 添加了 ISNULL,因为 NULL 值不计算在内。
  • 但我想数一数,如果你看到我的第三张桌子,很糟糕,但是,在第一张桌子上。
  • @sh0rt6r 更改了代码,假设您在表 1 中有所有可能的 idSection 和 idQuestion 组合。无论如何,UNION 是我所知道的获得您需要的东西的唯一方法。
【解决方案3】:

试试这个:

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

【讨论】:

  • 差不多了。但是,NULL 出现在 idSection 和 idQuestion 中,我不想要。想为每个 id 出现
  • 您需要向我们提供一个单独的表格,其中包含所有 idSections 和 idQuestions。那么这种类型的查询可以用UNION来解决
  • 可能有不同的idSection和idQuestion,这就是为什么要分组
猜你喜欢
  • 2017-10-19
  • 2016-03-29
  • 2015-06-21
  • 2013-07-06
  • 1970-01-01
  • 2020-08-04
  • 2011-04-15
  • 2018-03-23
  • 2013-04-26
相关资源
最近更新 更多