ms!:
awesome stuff - very helpful. works great.
could you do a favor talk me through
SELECT a_t.posts_content, GROUP_CONCAT((SELECT cat FROM cats WHERE cat_id = b_t.cat_id) SEPARATOR ', ') AS cat_names FROM posts AS a_t LEFT JOIN posts2cats b_t ON(a_t.posts_id = b_t.posts_id) GROUP BY a_t.posts_content ORDER BY a_t.posts_id;
real quick? (what's up with a_t and b_t?)
I'm just trying to understand better so I can learn from your great answer.
Main Topics
Browse All Topics





by: mensuckPosted on 2006-08-25 at 21:40:28ID: 17394554
If I am understanding you, then you can do something like below! mysql (5)
returns...
Showing rows 0 - 2 (3 total, Query took 0.0001 sec)
posts_content | cat_names
I like oatmeal | Breakfast, Opinion
Bacon + Eggs | Breakfast
Ninjas are awesome | Opinion, Truth
example....
SELECT a_t.posts_content, GROUP_CONCAT((SELECT cat FROM cats WHERE cat_id = b_t.cat_id) SEPARATOR ', ') AS cat_names FROM posts AS a_t LEFT JOIN posts2cats b_t ON(a_t.posts_id = b_t.posts_id) GROUP BY a_t.posts_content ORDER BY a_t.posts_id;
// tables
CREATE TABLE cats (
cat_id int(10) unsigned NOT NULL auto_increment,
cat varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (cat_id),
KEY cat (cat)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO cats VALUES (1, 'Breakfast');
INSERT INTO cats VALUES (2, 'Opinion');
INSERT INTO cats VALUES (3, 'Truth');
CREATE TABLE posts (
posts_id int(10) unsigned NOT NULL auto_increment,
posts_content varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (posts_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO posts VALUES (1, 'I like oatmeal');
INSERT INTO posts VALUES (2, 'Bacon + Eggs');
INSERT INTO posts VALUES (3, 'Ninjas are awesome');
CREATE TABLE posts2cats (
rel_id int(10) unsigned NOT NULL auto_increment,
posts_id int(10) unsigned NOT NULL,
cat_id int(10) unsigned NOT NULL,
PRIMARY KEY (rel_id),
KEY posts_id (posts_id,cat_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
INSERT INTO posts2cats VALUES (1, 1, 1);
INSERT INTO posts2cats VALUES (2, 1, 2);
INSERT INTO posts2cats VALUES (3, 2, 1);
INSERT INTO posts2cats VALUES (4, 3, 2);
INSERT INTO posts2cats VALUES (5, 3, 3);
ms!