0

I have 2 tables:

Table:skill
SkillID int(10) unsigned
Description varchar(100)

Table: question
QuestionID int(10) unsigned
SkillID int(10) unsigned
Details varchar(100)

There are many rows in the question table for each skill. I want to run one query listing all skills and only one question for that skill (does not matter which question).

How can that be done?

1
  • what if a particular skill has no equivalent in question table? Commented Jun 16, 2013 at 10:46

2 Answers 2

1

FOR MYSQL AND SQLSERVER

SELECT * 
FROM skill S 
INNER JOIN question Q ON S.SkillID=Q.SkillID 
INNER JOIN (
SELECT SkillID ,max(QuestionID) QuestionID
FROM question
GROUP BY SkillID)T
ON S.SkillID =T.SkillID AND Q.QuestionID=T.QuestionID

FOR MYSQL

SELECT SkillID ,max(QuestionID) QuestionID
FROM question
GROUP BY SkillID
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

SELECT SkillID, QuestionID -- or, whatever fields you would need
FROM question
GROUP BY SkillID
ORDER BY RAND()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.