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

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

How to get questions present on mutiple categories using sql?

问题

以下是您要翻译的内容:

  1. SELECT faq_categories.id AS category_id, faq_categories.categories_name, faq_questions.id,
  2. faq_questions.question, faq_questions.answer
  3. FROM faq_questions
  4. LEFT JOIN users ON users.certID = faq_questions.certID
  5. LEFT JOIN faq_categories ON faq_categories.id = faq_questions.categories
  6. WHERE faq_questions.certID LIKE ''%0%''
  7. and faq_questions.categories IN (1,2,3,4)
  8. GROUP BY faq_questions.question;
  9. This query gets questions based on categories and userCERTID.
  10. How to modify this query. For example to get questions present on multiple categories. For
  11. `Example if question 1 is in both category 1 and category 2. Then result should be category1
  12. question1 and category 2 question1`
  13. Existing
  14. category_name question
  15. Business q1
  16. Business q2
  17. Desired Result
  18. Category_name question
  19. Business q1
  20. Business q2
  21. new q1
  22. sample q2
  23. How to modify this query. For example to get questions present on multiple categories. For
  24. `Example if question 1 is in both category 1 and category 2. Then result should be category1
  25. question1 and category 2 question1`
英文:
  1. SELECT faq_categories.id AS category_id, faq_categories.categories_name, faq_questions.id,
  2. faq_questions.question, faq_questions.answer
  3. FROM faq_questions
  4. LEFT JOIN users ON users.certID = faq_questions.certID
  5. LEFT JOIN faq_categories ON faq_categories.id = faq_questions.categories
  6. WHERE faq_questions.certID LIKE '%0%'
  7. and faq_questions.categories IN (1,2,3,4)
  8. GROUP BY faq_questions.question;

This query gets questions based on categories and userCERTID.

  1. How to modify this query .For example to get questions present on mutiple categories. For
  2. `Example if question 1 is in both category 1 and category 2.Then result should be category1
  3. question1 and category 2 question1`
  4. Existing
  5. category_name question
  6. Business q1
  7. Business q2
  8. Desired Result
  9. Category_name question
  10. Business q1
  11. Business q2
  12. new q1
  13. 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:

确定