我有 2 张桌子:
Table:skill
SkillID int(10) unsigned
Description varchar(100)
Table: question
QuestionID int(10) unsigned
SkillID int(10) unsigned
Details varchar(100)
每个技能的问题表中有很多行。我想运行一个查询,列出所有技能,并且该技能只有一个问题(不管哪个问题)。
怎么可能呢?
FOR MYSQL 和 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
为MYSQL
SELECT SkillID ,max(QuestionID) QuestionID
FROM question
GROUP BY SkillID
你可以试试这个:
SELECT SkillID, QuestionID -- or, whatever fields you would need
FROM question
GROUP BY SkillID
ORDER BY RAND()