pingeyeg
asked on
Query statement not outputting what I want
The query I have come up with is not doing exactly what I want it to do. My objective is to have all most recent questions from all corresponding tables be output to the screen in order of their date. The issue I'm having right now is I would like for the question, that has not been answered, to show on the screen with a comment below it stating it has not been answered, but if the question has been answered, I want the comment below it to state the last time it was updated. Placing the comment below it is not the issue; the issue is displaying the question properly. Below are screen shots and the query.
Screen-shot-2010-01-01-at-10.51..png
SELECT answers.user, answers.answer, answers.qID, user_question.question, answers.date, user_question.id uqID, user_question.make, user_question.model, 'type' 'A'
FROM user_question
LEFT JOIN answers ON user_question.id = answers.qID
UNION SELECT beyond_question.user, beyond_question.answer, beyond_question.id bID, beyond_question.question, beyond_question.date, NULL , NULL , NULL , 'type' 'B'
FROM beyond_question
UNION SELECT how_to.user, how_to.answer, how_to.id hID, how_to.question, how_to.date, NULL , NULL , NULL , 'type' 'H'
FROM how_to
ORDER BY DATE DESC
LIMIT 6
Screen-shot-2010-01-01-at-10.51..pngScreen-shot-2010-01-01-at-10.51..png
SELECT answers.user, isnull(answers.answer,'Thi s has not been answered'), answers.qID, user_question.question, answers.date, user_question.id uqID, user_question.make, user_question.model, 'type' 'A'
FROM user_question
LEFT JOIN answers ON user_question.id = answers.qID
UNION
SELECT beyond_question.user, isnull(beyond_question.ans wer,'This has not been answered'), beyond_question.id bID, beyond_question.question, beyond_question.date, NULL , NULL , NULL , 'type' 'B'
FROM beyond_question
UNION SELECT how_to.user, how_to.answer, how_to.id hID, how_to.question, how_to.date, NULL , NULL , NULL , 'type' 'H'
FROM how_to
ORDER BY DATE DESC
LIMIT 6
FROM user_question
LEFT JOIN answers ON user_question.id = answers.qID
UNION
SELECT beyond_question.user, isnull(beyond_question.ans
FROM beyond_question
UNION SELECT how_to.user, how_to.answer, how_to.id hID, how_to.question, how_to.date, NULL , NULL , NULL , 'type' 'H'
FROM how_to
ORDER BY DATE DESC
LIMIT 6
Hope this is what you require:
1. TOP 1 record from First SELECT
2. TOP 1 records from second SELECT
3. TOP 4 records from Third SELECT
Just change the column names in ORDER BY clause before running the query..
1. TOP 1 record from First SELECT
2. TOP 1 records from second SELECT
3. TOP 4 records from Third SELECT
Just change the column names in ORDER BY clause before running the query..
SELECT answers.user, answers.answer, answers.qID, user_question.question, answers.date, user_question.id uqID, user_question.make, user_question.model, 'type' 'A'
FROM user_question
LEFT JOIN answers ON user_question.id = answers.qID
ORDER BY answers.date DESC
LIMIT 1
UNION
SELECT beyond_question.user, beyond_question.answer, beyond_question.id bID, beyond_question.question, beyond_question.date, NULL , NULL , NULL , 'type' 'B'
FROM beyond_question
ORDER BY some_column DESC
LIMIT 1
UNION
SELECT how_to.user, how_to.answer, how_to.id hID, how_to.question, how_to.date, NULL , NULL , NULL , 'type' 'H'
FROM how_to
ORDER BY how_to.date DESC
LIMIT 4
Syntax Mistake..
(SELECT answers.user, answers.answer, answers.qID, user_question.question, answers.date, user_question.id uqID, user_question.make, user_question.model, 'type' 'A'
FROM user_question
LEFT JOIN answers ON user_question.id = answers.qID
ORDER BY answers.date DESC
LIMIT 1)
UNION
(SELECT beyond_question.user, beyond_question.answer, beyond_question.id bID, beyond_question.question, beyond_question.date, NULL , NULL , NULL , 'type' 'B'
FROM beyond_question
ORDER BY some_column DESC
LIMIT 1)
UNION
(SELECT how_to.user, how_to.answer, how_to.id hID, how_to.question, how_to.date, NULL , NULL , NULL , 'type' 'H'
FROM how_to
ORDER BY how_to.date DESC
LIMIT 4);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here,
Then you say,
So,