I have two tables question and answers.
- questions{id,user_id,question,date}
- answers {id,q_id,user_id,answer, date}
I want to retrieve the questions and answers that have been inputted by the same user
i.e. select all answer and all questions for ID=39 order by date DESC
and also after I have a query, and I’m wanting to while through the fetch array, and displaying the data, how can I distinguish whether it was a question or an answer, so I can display them correctly.
EDIT:
SELECT 'Q' AS
TYPE , q.question AS value, q.date
FROM questions q
WHERE q.user_id =39
UNION ALL SELECT 'A' AS
TYPE , q.question AS value, a.date
FROM answers a,questions q
WHERE a.q_id = q.id
AND
WHERE a.user_id =39
ORDER BY `date` DESC
im sorry but im trying to get the question that has been answered rather then the answer itself. i updated the sql and the database design on top, but i keep getting an error
Use:
UNIONandUNION ALLare used to combine queries to return a single result set.UNIONremoves duplicates;UNION ALLdoes not remove duplicates and is faster thanUNIONbecause of this. But there needs to be the same number of columns in the SELECT clauses of all the SELECT statements in a UNION’d statement. And their data types have to match at each position.To differentiate between answers and question values, the example defined static values “A” to stand for answers, and “Q” to stand for questions. Your application layer code can work off these to format the data as desired.