I have main query:
SELECT mt.media_id, mt.title,
SUM(DISTINCT sht.share) as share_count
FROM $grouped_media_table mt
LEFT JOIN $share_table sht ON sht.media_id = mt.media_id AND sht.title = mt.title
WHERE mt.media_id = %d AND mt.title = %s
GROUP BY mt.media_id, mt.title
grouped_media_table :
CREATE TABLE $grouped_media_table (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`media_id` int(11) unsigned DEFAULT NULL,
`title` varchar(300) DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `title` (`title`)
);
share_table
CREATE TABLE $share_table (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`share` int(11) unsigned DEFAULT '0',
`media_id` int(11) unsigned DEFAULT NULL,
`user_id` int(11) unsigned DEFAULT NULL,
`title` varchar(300) DEFAULT NULL,
`c_date` datetime,
PRIMARY KEY (`id`),
INDEX `media_id` (`media_id`)
);
This returns one row for media_id=5 and title="foo"
And another query:
SELECT ct.id, ct.comment, ct.comment_parent_id, ct.reported, ct.user_id, ct.user_display_name, ct.avatar, ct.c_date, SUM(vt.vote) AS vote, MAX(vt.user_id = %d) AS user_voted,
MAX(CASE WHEN vt.user_id = %d THEN vote END) AS user_vote
FROM $comments_table as ct
LEFT JOIN $votes_table vt on ct.id = vt.comment_id
WHERE ct.media_id=%d AND ct.title=%s
GROUP BY ct.id
ORDER BY ct.c_date DESC
This returns all rows for media_id=5 and title="foo"
comments_table :
CREATE TABLE $comments_table (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`media_id` int(11) unsigned DEFAULT NULL,
`user_id` int(11) unsigned DEFAULT NULL,
`user_display_name` varchar(100) DEFAULT NULL,
`avatar` varchar(300) DEFAULT NULL,
`origtype` varchar(20) DEFAULT NULL,
`title` varchar(300) DEFAULT NULL,
`url` varchar(300) DEFAULT NULL,
`c_date` datetime,
`comment` longtext DEFAULT NULL,
`comment_parent_id` int(11) unsigned DEFAULT NULL,
`reported` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`),
INDEX `media_id` (`media_id`)
);
votes_table:
CREATE TABLE $votes_table (
`comment_id` int(11) unsigned DEFAULT NULL,
`user_id` int(11) unsigned DEFAULT NULL,
`vote` tinyint(1) DEFAULT 0,
INDEX `comment_id` (`comment_id`)
);
My queries work well on their own. Is there a way to combine this into single query?
I tried the following, but it only returns one row (comment) from comments_table (while it should return all comments for media_id=5 and title="foo"). share_count is correctly returned so this join works well.
SELECT mt.media_id, mt.title, SUM(DISTINCT sht.share) as share_count, ct.comment
FROM $grouped_media_table mt
LEFT JOIN $share_table sht ON sht.media_id = mt.media_id AND sht.title = mt.title
LEFT JOIN (SELECT ct.id, ct.media_id, ct.title, comment, ct.comment_parent_id, ct.reported, ct.user_id, ct.user_display_name, ct.avatar, ct.c_date, SUM(vt.vote) AS vote,
MAX(vt.user_id = %d) AS user_voted,
MAX(CASE WHEN vt.user_id = %d THEN vote END) AS user_vote
FROM $comments_table as ct
LEFT JOIN $votes_table vt on ct.id = vt.comment_id
WHERE ct.media_id = %d AND ct.title=%s
GROUP BY ct.id
ORDER BY ct.c_date DESC
) as ct ON ct.media_id = mt.media_id AND ct.title = mt.title
WHERE mt.media_id = %d AND mt.title = %s
GROUP BY mt.media_id, mt.title
Note - I need all comment data returned, but now for clarity I just use SELECT mt.media_id, mt.title, ct.comment
SELECT .. FROM ... CROSS JOIN (query 2) AS query2 WHERE ...ct.comment, butct.commentis not aggregated and is not part of the GROUP BY clause withmt.media_idandmt.title