英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论