我需要参加3个不同的MySQL表连接:SQL多带计数
- 的问题表(代表questio)
- 的问题投票表格(表示喜欢的问题时,登录= 1不同于当Sign = 0时)
- 答案表(表示具有对特定问题的答案的表)。
这三个表有这样的结构:
Questions
--------------------------------------------
| ID | UserID | Title | Body | Date
--------------------------------------------
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016
| 2 | 45 | Who are we? | BodyQuestion | 02/02/2016
--------------------------------------------
QuestionsVotes
--------------------------------------------
| ID | QuestionID | Sign
--------------------------------------------
| 1 | 1 | 1
| 2 | 2 | 1
| 3 | 1 | 0
| 4 | 1 | 1
| 5 | 2 | 0
Answers
--------------------------------------------
| ID | QuestionID | UserID | Body
--------------------------------------------
| 1 | 1 | 45 | Yes, it is.
| 2 | 2 | 10 | Tricky question...
| 3 | 1 | 67 | In Barcelona it is not
我需要的是返回一个SQL查询,给出问题的ID,以尽可能多的行返回的答案的问题已收到数每一个都与Questions.ID,Questions.UserID,Questions.Title,Questions.Body,Questions.Date,与问题相关的喜欢,与问题相关的unlikes,Answers.ID,Answers.UserID。如您所见,行之间的唯一更改是Answers字段。
SQL Output (for Question with ID=1)
--------------------------------------------
| ID | UserID | Title | Body | Date | Likes | Unlikes | AnswerID | AnswerUserID
--------------------------------------------
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016 | 2 | 1 | 1 | 45
| 1 | 32 | Is it raining? | BodyQuestion | 01/01/2016 | 2 | 1 | 3 | 67
EDIT 1:实施例给出。
请提供采样数据和确切的希望的输出。 –
你可以编辑这个问题,并澄清你在找什么。我读了你的底部段落三次,仍然没有真正得到你所要求的输出。 –
我已经添加了一个完整的例子。 – David103