0

我有两张桌子

table 1                             table 2
id   q_id   content                 id    w_id     q_id    c_id    ranking
----------------------          ------------------------------------------------
95   2046   1=E                     123   22404    2046    100     0
96   2046   2=G                     124   22404    2046    101     1
97   2046   3=N                     125   22404    2046    102     1
98   2046   4=B                     126   22404    2046    103     1
99   2046   5=V                     127   22404    2046    104     2
100  2046   A1                      128   22404    2046    105     2
101  2046   A2                      129   22505    2046    A1      0
102  2046   A3                      130   22505    2046    A2      2
103  2046   A4                      131   22505    2046    A3      1
104  2046   A5                      132   22505    2046    A4      2
105  2046   A6                      133   22505    2046    A5      3
106  2046   A7                      134   22505    2046    A6      3
-----------------------             135   22505    2046    A7      0

我要执行到这种格式(这里没有计算sd)

id     content     av     total     E     G     N     B     V     sd
----------------------------------------------------------------------
100      A1        1        2       2     0     0     0     0      0
101      A2       1.5       2       0     1     1     0     0      0
102      A3        1        2       1     1     0     0     0      0
103      A4        2        2       0     2     0     0     0      0
104      A5        3        2       0     0     2     0     0      0
105      A6        3        2       0     0     2     0     0      0
106      A7        3        1       0     0     1     0     0      0

根据 Mark 的建议修改了代码:

SELECT *
FROM table_1 t1
JOIN table_2 t2 ON t2.c_id in (t1.id, t1.content)
WHERE t1.q_id = 2046 AND 
  t2.q_id = 2046 AND 
  t2.ranking >= 0 AND 
  t2.w_id IN (22404, 22505) 
GROUP BY t1.id

结果:

id    q_id    content    w_id    c_id    ranking
-------------------------------------------------------
100   2046    A1         22404    100      0
101   2046    A2         22404    101      1
102   2046    A3         22404    102      1
103   2046    A4         22404    103      1
104   2046    A5         22404    104      2
105   2046    A6         22404    105      2
106   2046    A7         22505    A1       1

第106行的c_id应该是A7而不是A1,排名也是错误的。因为我有 20 行(A1-A20)。A7-A20 的行错了。A7-A20 的 c_id 和排名都变成了 A1 和 1。

4

1 回答 1

0

除了 Noel 确定的解决方案之外,还值得一提的是,22505 条记录似乎需要在匹配 table_1 的内容而不是 id 的 c_id 上进行连接,您有一个不必要的连接,并且您现有的查询混合了隐式和显式连接 - 相反,尝试这个:

SELECT t1.id, 
       t1.content,
       AVG(t2.ranking+1) AS av, 
       COUNT(t2.w_id) AS total, 
       COUNT(IF(ranking=0,1,NULL)) AS E,
       COUNT(IF(ranking=1,1,NULL)) AS G, 
       COUNT(IF(ranking=2,1,NULL)) AS N, 
       COUNT(IF(ranking=3,1,NULL)) AS B,
       COUNT(IF(ranking=4,1,NULL)) AS V, 
       STDDEV(ranking) AS sd
FROM table_1 t1
JOIN table_2 t2 ON t2.c_id in (t1.id, t1.content)
WHERE t1.q_id = 2046 AND 
      t2.q_id = 2046 AND 
      t2.ranking >= 0 AND 
      t2.w_id IN (22404, 22505) 
GROUP BY t1.id

SQLFiddle在这里

于 2013-05-06T10:16:11.083 回答