如何使用SQL获取存在于多个类别中的问题?

huangapple go评论56阅读模式
英文:

How to get questions present on mutiple categories using sql?

问题

以下是您要翻译的内容:

SELECT faq_categories.id AS category_id, faq_categories.categories_name, faq_questions.id, 
faq_questions.question, faq_questions.answer
FROM faq_questions
LEFT JOIN users ON users.certID = faq_questions.certID
LEFT JOIN faq_categories ON faq_categories.id = faq_questions.categories 
WHERE faq_questions.certID LIKE ''%0%''
and faq_questions.categories IN (1,2,3,4)
GROUP BY faq_questions.question;
This query gets questions based on categories and userCERTID.

How to modify this query. For example to get questions present on multiple categories. For 
`Example if question 1 is in both category 1 and category 2. Then result should be category1 
question1 and category 2 question1`

Existing
category_name   question
Business          q1
Business          q2

Desired Result
Category_name   question
Business          q1
Business          q2
new               q1
sample            q2

How to modify this query. For example to get questions present on multiple categories. For 
`Example if question 1 is in both category 1 and category 2. Then result should be category1 
question1 and category 2 question1`
英文:
 SELECT faq_categories.id AS category_id, faq_categories.categories_name, faq_questions.id, 
 faq_questions.question, faq_questions.answer
 FROM faq_questions
   LEFT JOIN users ON users.certID = faq_questions.certID
   LEFT JOIN faq_categories ON faq_categories.id = faq_questions.categories 
   WHERE faq_questions.certID LIKE '%0%'
   and faq_questions.categories IN (1,2,3,4)
   GROUP BY faq_questions.question;

This query gets questions based on categories and userCERTID.

 How to modify this query .For example to get questions present on mutiple categories. For 
   `Example if question 1 is in both category 1 and category 2.Then result should be category1 
   question1 and category 2 question1`

Existing
category_name   question
Business          q1
Business          q2

Desired Result
Category_name   question
Business          q1
Business          q2
new               q1
sample            q2

How to modify this query .For example to get questions present on mutiple categories. For
Example if question 1 is in both category 1 and category 2.Then result should be category1
question1 and category 2 question1

答案1

得分: 1

为了实现这一点,您需要另一个像category_questions这样的透视表来存储category_id和question_id,使用foreach。然后,您可以使用左连接透视表检索出现在多个类别中的问题。

英文:

To acheive this you need another pivot table like category_questions to store
category_id and question_id using foreach.Then you can retreive questions present
on mutiple categories using left join pivot table.

huangapple
  • 本文由 发表于 2023年5月11日 17:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定