2015-06-18 49 views
-1

我有两个表。 “question_answers”有列questionanswer。 “question_attempts”具有questionidresponsesummary从两个表中计数匹配记录

例如:question = 4有5条记录来自“question_answers”。在“question_attempts” 同questionid查找和获取的匹配记录的次数计数“question_attempts”

“question_answers” enter image description here “question_attempts” enter image description here

+0

你尝试过什么了吗?你应该可以(内部)加入这两个表并且计数 – Ali

+0

不使用内部连接。echo $ data = mysql_num_rows(“从question_attempts'选择count('respondummary'),其中'questionid' = 4 AND'respondummary' = 43152”); 这是我写的代码。它在phpmyadmin中显示, – JRK

+0

但是,尝试在页面中显示时不显示。 – JRK

回答

1

这将是容易得多,如果我能有一个样本这里这些表的,但试试这个

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; 

,如果它工作正常,你应该得到的答案ID的表,尝试对这个问题的答案的数目。

但是您需要在这两个字段中完全匹配。从question_attempts和question_answers的responsessummary回答问题。

,如果它不可能解决方法是使用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; 
+0

没有显示。 请你指导我如何通过这个文件发送,我会寄给你那两张表。 – JRK

+0

或者给我举例question = 1 – JRK

+0

不需要发送文件,我认为你应该能够复制你的两个表的创建查询以及一些插入查询来在每个表中添加一些数据。如果你需要添加更多的过滤器,只需在“GROUP BY”之前添加一个** WHERE **子句,例如'WHERE question_answers.question = 1' – Ali

相关问题