mysql - Get all posts from a specific category -


the situation

as of might know previous questions, i'm developing blog-system.

this time, i'm stuck @ getting posts specific category, category.

database

here sql-commands create 3 required tables.

post

create table post(     headline varchar(100),     date datetime,     content text,     author int unsigned,     public tinyint,     type int,     id serial,     primary key (id), )engine=innodb default charset=utf8 collate=utf8_unicode_ci; 

author id of user created post, public determines if post can read or draft , type determines if it's blog-post (0) or else.

category

create table kategorie(     name varchar(30),     short varchar(200),     id serial,     primary key (name) )engine=innodb default charset=utf8 collate=utf8_unicode_ci; 

post_kategorie

create table post_kategorie(     post_id bigint unsigned,     kategorie_id bigint unsigned,     primary key (post_id, kategorie_id),     foreign key (post_id) references post(id),     foreign key (kategorie_id) references kategorie(id) )engine=innodb default charset=utf8 collate=utf8_unicode_ci; 

the query

this current query posts tagged specific category, determined category's id:

select post.headline, post.date, post.id,   concat(     "[", group_concat('{"name":"',kategorie.name,'","id":',kategorie.id,'}'), "]"   ) "categorys" post inner join post_kategorie   on post.id = post_kategorie.post_id inner join kategorie   on post_kategorie.kategorie_id = 2 post.public = 1   , post.type = 0 group post.headline, post.date order post.date desc limit 0, 20 

the query works listing posts tagged specific category, categorys-column gets mixed every listed post has available category's (every category listed in kategorie-table).

i'm sure problem lays in inner join-condition, have no clue where. please point me in right direction.

i suspect there might issues concat function, mixes different types of quotation marks. think "[" , "]" should respectively '[' , ']'.

otherwise, problem seem 1 of joins. in particular, inner join kategorie not specify joining condition, which, think, should post_kategorie.kategorie_id = kategorie.id.

there entire query should this:

select post.headline, post.date, post.id,   concat(     "[", group_concat('{"name":"',kategorie.name,'","id":',kategorie.id,'}'), "]"   ) "categorys" post inner join post_kategorie   on post.id = post_kategorie.post_id inner join kategorie   on post_kategorie.kategorie_id = kategorie.id post.public = 1   , post.type = 0 group post.headline, post.date having max(case post_kategorie.kategorie_id when 2 1 else 0 end) = 1 order post.date desc limit 0, 20 

the post_kategorie.kategorie_id = 2 condition has been modified case expression , moved having clause, , used max() aggregate function. works follows:

  • if post tagged tag or tags belonging kategorie.id = 2, case expression return 1, , max evaluate 1 too. consequently, group valid , remain in output.

  • if no tag post tagged belongs said category, case expression never evaluate 1, nor max. result, entire group discarded.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -