- Aug 20, 2000
- 20,577
- 432
- 126
Hey guys - I'm a little stumped with putting together this MySQL query to count the number of comments made to both a user's blog and their photo album. I've got no issues using LEFT OUTER JOINs or GROUP BY clauses, but the two of them together have run me into some trouble.
If I do the below, which is the query put together to the best of my knowledge, I end up with a Cartesian product for an answer. What can I do to fix that?
The query (MySQL):
SELECT au.username,
au.name_first,
au.name_last,
au.account_id,
au.city,
COUNT(mec.media_file_comment_id) AS num_comments_m,
COUNT(bec.blog_entry_comment_id) AS num_comments_b
FROM account_user au
LEFT OUTER JOIN media m, media_folder mf, media_file mfile, media_file_comment mec
ON (au.account_id = m.account_id AND m.media_id = mf.media_id AND mf.media_folder_id = mfile.media_folder_id AND mfile.media_file_id = mec.media_file_id)
LEFT OUTER JOIN blog b, blog_entry be, blog_entry_comment bec
ON (au.account_id = b.account_id AND b.blog_id = be.blog_id AND be.blog_entry_id = bec.blog_entry_id AND m.account_id = b.account_id)
GROUP BY au.account_id
LIMIT 10
The schema (MySQL):
CREATE TABLE account_user (
user_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE blog (
blog_id INTEGER NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE blog_entry (
blog_entry_id INTEGER NOT NULL AUTO_INCREMENT,
blog_id INTEGER);
CREATE TABLE blog_entry_comment (
blog_entry_comment_id INTEGER AUTO_INCREMENT NOT NULL,
blog_entry_id INTEGER NOT NULL);
CREATE TABLE media (
media_id INTEGER NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE media_folder (
media_folder_id INTEGER NOT NULL AUTO_INCREMENT,
media_id INTEGER NOT NULL);
CREATE TABLE media_file (
media_file_id INTEGER NOT NULL AUTO_INCREMENT,
media_folder_id INTEGER NOT NULL);
CREATE TABLE media_file_comment (
media_file_comment_id INTEGER NOT NULL AUTO_INCREMENT,
media_file_id INTEGER NOT NULL);
If I do the below, which is the query put together to the best of my knowledge, I end up with a Cartesian product for an answer. What can I do to fix that?
The query (MySQL):
SELECT au.username,
au.name_first,
au.name_last,
au.account_id,
au.city,
COUNT(mec.media_file_comment_id) AS num_comments_m,
COUNT(bec.blog_entry_comment_id) AS num_comments_b
FROM account_user au
LEFT OUTER JOIN media m, media_folder mf, media_file mfile, media_file_comment mec
ON (au.account_id = m.account_id AND m.media_id = mf.media_id AND mf.media_folder_id = mfile.media_folder_id AND mfile.media_file_id = mec.media_file_id)
LEFT OUTER JOIN blog b, blog_entry be, blog_entry_comment bec
ON (au.account_id = b.account_id AND b.blog_id = be.blog_id AND be.blog_entry_id = bec.blog_entry_id AND m.account_id = b.account_id)
GROUP BY au.account_id
LIMIT 10
The schema (MySQL):
CREATE TABLE account_user (
user_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE blog (
blog_id INTEGER NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE blog_entry (
blog_entry_id INTEGER NOT NULL AUTO_INCREMENT,
blog_id INTEGER);
CREATE TABLE blog_entry_comment (
blog_entry_comment_id INTEGER AUTO_INCREMENT NOT NULL,
blog_entry_id INTEGER NOT NULL);
CREATE TABLE media (
media_id INTEGER NOT NULL AUTO_INCREMENT,
account_id INTEGER);
CREATE TABLE media_folder (
media_folder_id INTEGER NOT NULL AUTO_INCREMENT,
media_id INTEGER NOT NULL);
CREATE TABLE media_file (
media_file_id INTEGER NOT NULL AUTO_INCREMENT,
media_folder_id INTEGER NOT NULL);
CREATE TABLE media_file_comment (
media_file_comment_id INTEGER NOT NULL AUTO_INCREMENT,
media_file_id INTEGER NOT NULL);