我有一个表来存储调查的答案,我试图查询mysql告诉我某个调查是否被答案字段= 16或20的人回答
我有以下的mysql语句
SELECT DISTINCT(submissionID) as submissionID FROM answer
WHERE EXISTS (
SELECT *
FROM answer
WHERE submissionID = submissionID AND (answer = '16' OR answer = '20')
)
在这里编辑
抱歉,我不认为我的问题是正确的,所以这是我的表结构:
CREATE TABLE IF NOT EXISTS `answer` (
`aID` int(11) NOT NULL AUTO_INCREMENT,
`sID` int(11) NOT NULL,
`qtID` int(11) NOT NULL,
`answer` text NOT NULL,
`userIP` text NOT NULL,
`submissionID` int(11) NOT NULL,
PRIMARY KEY (`aID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=422 ;
aID
是给出的每个答案的唯一ID。sID
是调查ID(因为我可能有不止一次调查)qtID
是questionsID值answer
是用户对此调查的qtID
给出的答案userIP
可自我解释submissionID
是每次提交的ID 所以我知道在这个表中有5个唯一的submitID,但只有一个submssionID
答案字段= 16
和20
答案 0 :(得分:1)
我认为您的查询过于复杂,请尝试以下方法:
SELECT DISTINCT submissionID FROM answer
WHERE answer IN ('16', '20');
答案 1 :(得分:0)
“我的故障是什么?”
将子查询与外部查询相关联的条件:
submissionID = submissionID
总是如此。我不认为你真的需要它,为什么不写:
SELECT DISTINCT submissionID
FROM answer
WHERE answer = '16' OR answer = '20'
但是,我觉得你没有给我们所有的信息。例如,我在原始查询中看不到任何限制特定问题的答案。如果调查只有一个问题,那当然是可能的,但我觉得这不是你的意思。精心...
答案 2 :(得分:0)
我认为您需要在子句中的一个或另一个中为答案表添加别名。像这样......
SELECT DISTINCT(submissionID) as submissionID FROM answer a
WHERE EXISTS (
SELECT *
FROM answer
WHERE submissionID = a.submissionID AND (answer = '16' OR answer = '20')
)
答案 3 :(得分:0)
这是您的原始查询
SELECT DISTINCT(submissionID) as submissionID FROM answer
WHERE EXISTS (
SELECT *
FROM answer
WHERE submissionID = submissionID AND (answer = '16' OR answer = '20')
)
您应该为表添加别名以将它们关联起来
SELECT DISTINCT(submissionID) as submissionID FROM answer B
WHERE EXISTS (
SELECT *
FROM answer A
WHERE A,submissionID = B.submissionID AND (answer = '16' OR answer = '20')
)