这是我的架构:
Table "Questoes"; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | id_quest | int(11) | NO | | NULL | | | questao | varchar(255) | NO | | NULL | | | nivel | int(11) | NO | | NULL | | | tipo | varchar(255) | NO | | NULL | | +----------+--------------+------+-----+---------+----------------+ Table "Respostas"; +----------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | id_quest | int(11) | NO | | NULL | | | resposta | varchar(255) | NO | | NULL | | | r_valido | enum('0','1') | NO | | NULL | | +----------+---------------+------+-----+---------+----------------+
我的查询是:
SELECT q.questao, r.resposta
FROM questoes q, respostas r
WHERE q.id_quest IN (19,20,21)
AND q.id_quest=r.id_quest
AND r.r_valido = ( SELECT resposta FROM respostas WHERE r_valido= 1 )
我需要的是来自表格questao
的字段Questoes
和来自表格resposta
的字段respostas
,其中字段为r_valido = 1
。
字段resposta
有4个结果,只有一个有效,换句话说,字段r_valido = 1
。
答案 0 :(得分:1)
您的查询应如下所示:
SELECT q.questao, r.resposta FROM questoes AS q JOIN respostas AS r ON r.id_quest = q.id_quest WHERE q.id_quest IN (19,20,21) AND r.r_valido = "1"
此外,我在查询中使用1
代替"1"
时发现了导致这种奇怪错误的原因:
我们强烈建议您不要使用数字作为枚举 值,因为它不会在适当的存储上节省 TINYINT或SMALLINT类型,很容易混合字符串和 如果引用,则可以使用基础数字值(可能不相同) ENUM值不正确
答案 1 :(得分:1)
我完全不理解你,但我认为这就是你要找的东西:
SELECT q.questao, r.resposta
FROM questoes as q
INNER JOIN respostas as r
ON q.id_quest=r.id_quest
WHERE
q.id_quest IN (19,20,21) AND
r.r_valido = '1'