[英]Count matching records from two tables
I have two tables.我有两张桌子。 "question_answers" having columns
question
and answer
. “question_answers”有列
question
和answer
。 "question_attempts" having questionid
and responsesummary
. “question_attempts”具有
questionid
和responsesummary
。
Example : question
=4 having 5 records from "question_answers".示例:
question
=4 有 5 个来自“question_answers”的记录。 Same questionid
in the "question_attempts" and find and get the count of matching records count in "question_attempts" “question_attempts”中的相同
questionid
并在“question_attempts”中查找并获取匹配记录计数
"question_answers" “问题_答案”
"question_attempts"
“问题尝试”
It would be much easier if i could have a sample of those tables here, but give this a try如果我能在这里得到这些表的样本会容易得多,但请尝试一下
SELECT
answers.id AS 'answer id',
count(attempts.id) AS 'number of attempts'
FROM
question_attempts AS attempts
JOIN
question_answers AS answers ON answers.answer = attempts.responsessummary
WHERE
question_answers.question = 1
GROUP BY
answers.id;
if it works correctly you should receive a table of answer ids with the number of attempts for that answer.如果它工作正常,您应该收到一个包含该答案尝试次数的答案 ID 表。
BUT you need to have exact match in those two fields.但是您需要在这两个字段中进行完全匹配。 answer from question_attempts and responsessummary from question_answers.
来自 question_attempts 的答案和来自 question_answers 的响应摘要。
if it not possible the workaround is to use LIKE clause :如果不可能,解决方法是使用LIKE子句:
SELECT
answers.id AS 'answer id',
count(attempts.id) AS 'number of attempts'
FROM
question_attempts AS attempts
JOIN
question_answers AS answers ON answers.answer
LIKE CONCAT('%', attempts.responsessummary , '%')
WHERE
question_answers.question = 1
GROUP BY
answers.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.