mysql query returning double rows -
i using standard apache2 lamp configuration (mysql , php5), , utf8 encoding.
i have set 4 tables.
the first 1 named articles
, has 6 columns:
id int(11) auto_increment primary key, title varchar(1000), posted (timestamp) author_id int(11), extract text, body text
the second 1 named authors
id int(11) auto increment primary key, name varchar(100), img varchar(100), bio text
the third 1 named categories
id int(2) auto_increment primary key, cat_name varchar(100)
and fourth 1 named article_categories
id int(2) auto_increment primary key, article_id int(11), category_id int(2)
now want write mysql query find title, author name , cat_name of article. far i've come this, , returns 10 rows, categories i've set up. (i've set 1 article 2 categories, , 5 categories in categories table).
this query:
select articles.title, authors.name, categories.cat_name articles, authors, categories, article_categories title '%introduction%';
and result:
+------------------------+-----------------------+--------------+ | title | name | cat_name | +------------------------+-----------------------+--------------+ | introduction ptc | yannick Šušteršič | free | | introduction ptc | yannick Šušteršič | free | | introduction ptc | yannick Šušteršič | members-only | | introduction ptc | yannick Šušteršič | members-only | | introduction ptc | yannick Šušteršič | milestone | | introduction ptc | yannick Šušteršič | milestone | | introduction ptc | yannick Šušteršič | basic | | introduction ptc | yannick Šušteršič | basic | | introduction ptc | yannick Šušteršič | advanced | | introduction ptc | yannick Šušteršič | advanced | +------------------------+-----------------------+--------------+
is there way display article once, corresponding categories name in single row?
if not, how relevant results? doing wrong? thanks
you forget compare author , articles vs categories :
select articles.title, authors.name, group_concat(categories.cat_name) articles, authors, categories, article_categories title '%introduction%' , articles.author_id = authors.id , articles.id = article_categories.article_id , article_categories.category_id = categories.id;
updated, final correct statement, authored op.
Comments
Post a Comment