Answer the question
In order to leave comments, you need to log in
How to remove articles from categories?
Good afternoon. There are tables:
blog
id
name
text
date
status
category
id
name
categorylist
id
category_id
blog_id
The categorylist table links articles to categories. One article can be placed in several categories.
How to make a selection of articles knowing the category in which it is located.
First, I select all article ids by category id:
SELECT `blog_id` FROM `categorylist` WHERE `category_id` = 1
SELECT * FROM `blog` WHERE `id` IN (1,2,3,4,5,6,7,8,9)
Answer the question
In order to leave comments, you need to log in
Read about JOINs
something like this:
select b.* from blog b
join categorylist cl ON b.id = cl.blog_id
join category c ON c.id = cl.category_id
where ....
And so, in order:
select b.* from 269816_blog b
where b.id in
(select cl.blog_id from 269816_categorylist cl where cl.category_id=1);
select b.* from 269816_blog b
where b.id in
(select cl.blog_id from 269816_categorylist cl
where cl.category_id=(select cat.id from 269816_category cat where cat.name=2001));
select b.*, group_concat(c.id)
from
269816_blog b,
269816_category c,
269816_categorylist cl
where cl.category_id=c.id
and cl.blog_id=b.id
group by b.id
group_concat(c.id)
to group_concat(c.name)
, then the result will be the names of the categories.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question