我想做个调查,首先这是我的桌子
PollTable
[pid][title][choices]选项是动态的,至少需要两个选项,它是用分隔符构造的。
示例:
'Apple|Grape|Banana'另一个表是响应表,我在其中存储用户响应。
[pid][uid][answer]示例数据:
1-1-1 //where pid - uid - answer
1-2-1
1-3-2
1-4-3
1-5-3使用这个查询,我可以得到每个答案的响应数。
SELECT `answer`, COUNT(`answer`) as 'response'
FROM `poll_response` WHERE `pid` = 1 GROUP BY `answer`结果
[ answer ][ response ]
[ 1 ][ 2 ]
[ 2 ][ 1 ]
[ 3 ][ 2 ]这很好,除非在没有人选择的情况下。
示例:
1-1-1
1-2-1
1-3-1
1-4-3
1-5-3这会让我
[ answer ][ response ]
[ 1 ][ 3 ]
[ 3 ][ 2 ]但我需要的是
[ answer ][ response ]
[ 1 ][ 3 ]
[ 2 ][ 0 or null ]
[ 3 ][ 2 ]我知道,如果每个选项都有自己的列,则可以使用投票表的左联接来完成这一操作,但同样,选择是动态的,可以超过10或任何X。
用当前的设置可以吗?有什么想法吗?
发布于 2014-09-11 09:24:40
你的问题中有两个有趣的问题。一个是如何确定选择的数量,另一个是如何在不创建一个表的情况下生成一个数字序列,您可以使用该表来查找“缺失”答案。
首先,一些字符串操作可以帮助您。如果分隔符始终为“AC.26”,则表达式length(choices)-length(replace(choices,"|",""))+1会为您提供投票的选择数。
关于第二个问题,可能有一种使用内联变量(@row等)的方法。要生成序列,但是在有有限的最大选择集的情况下,一种更简单的方法可能是一个简单的联合,例如,按照select 1 as counter union select 2 union select 3 union select 4 union select 5的思路。
结合上面的内容,从响应表中找出没有收到答复的序列中的项目,并使用not exists。使用字符串表达式将从序列中的项限制为小于给定轮询可用的最大选项数的项。例如:
select counter as answer, 0 as response
from polltable p,
(select 1 as counter union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) as options
where p.pid=1
and not exists (
select answer from response r
where r.pid=1
and answer=counter)
and counter<=length(choices)-length(replace(choices,"|",""))+1最后,将有答案的初始查询与上面的内容结合起来。这是一个示例sql小提琴。
发布于 2014-09-11 07:58:35
我认为这不可能..。
但是,您认为这种使用javascript的方法怎么样?这合理吗?
数据在json中传递,所以
是我干的
choices = data[0]['choices'].split('|'); //Where data is the SELECT * for the Poll Table而现在
["Apple" , "Grape", "Banana"]现在,我这样做是为了制作一个容器作为响应。
var response = new Array( choices.length+1 ).join('0').split('').map(parseFloat);它有
[ 0, 0, 0 ]在这个循环中输入
for( x=0; x<responsedata.length; x++ ){
response[ parseInt( responsedata[x]['answer']-1 ) ] = parseInt( responsedata[x]['response'] );
//Where responsedata is the result of
//SELECT `answer`, COUNT(`answer`) as 'response'
//FROM `poll_response` WHERE `pid` = 1 GROUP BY `answer`
}所以现在我把这个放在我的响应数组中
[3, 0, 2]https://stackoverflow.com/questions/25781484
复制相似问题