Reputation: 427
I have two table (question table and answer table).
Question Table
id question
1 what is your name?
2 how old are you?
Answer Table
id answer questionid
1 john 1
2 smith 1
3 waiyan 1
4 20 2
5 21 2
6 22 2
When I selected records,The results is following.
SELECT * FROM question INNER JOIN answer on questionid= question.id
what is your name?
john
what is your name?
smith
what is your name?
waiyan
how old are you?
20
how old are you?
21
how old are you?
22
I want to remove duplicate question statement. I want to get following.
What is your name?
John
smith
waiyan
how old are you?
20
21
22
Please Someone help me how to write it. I am really beginner for mysql. So sorry for my bad english usage.
Upvotes: 2
Views: 1373
Reputation: 7056
The issue is in your php code, not your MySQL statements. You need to only print the question when it's different from the previous question. There are lots of ways to do this. Keep in mind that in your SQL query, you'll need to do:
SELECT * FROM question INNER JOIN answer
on questionid= question.id
order by question.id
To be sure that answers for the same question are not repeated. Then do something like the following (thanks to @drrcknlsn for fixing my attempt at php):
$last_question = "";
foreach ($results as $row) {
if ($last_question !== $row["question.question"]) {
echo $row["question.question"];
}
echo $row["answer.answer"];
$last_question = $row["question.question"];
}
Upvotes: 2