mysql请求来自两个表advanced2

时间:2016-02-11 00:43:50

标签: php mysql

我有两个mysql表:

**tbl_questions**
id user_id title  solved_answer
1  1       Title1 0               // Not solved question
2  2       Title2 3               // Solved question

**tbl_answers**
id user_id title   
1  3       Answer1 
2  4       Answer2 
3  4       Answer3 

我需要通过user_id = 4

计算已解决答案的问题数量

我该怎么做?

1 个答案:

答案 0 :(得分:1)

此查询将返回所有已解决的答案(考虑到您使用答案表中的答案ID更新问题)

select * from question q
join answer a on q.solved_answer = a.id

这个将搜索特定用户的特定问题的答案:

select * from question q
join answer a on q.solved_answer = a.id
where a.user_id=4