mysql 如何使用sql获取多个类别的问题?

14ifxucb  于 2023-05-28  发布在  Mysql
关注(0)|答案(1)|浏览(141)
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;

此查询基于类别和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

如何修改此查询。例如,获取有关多个类别的问题。对于Example if question 1 is in both category 1 and category 2.Then result should be category1 question1 and category 2 question1

nwlls2ji

nwlls2ji1#

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

相关问题