Hi,
I have 2 MySQL problems which I’m hoping someone can give me some advice with.
#1
I’m trying to develop a related posts feature for the blog section on my site, one way of doing this would be to use %LIKE% but that’s not really the most practical. I’m trying to use a boolean query and sort the results based on the relevance against the search terms. I’ve tried 2 different methods but as I’ve never done this type of query before I’m not sure which is right - seen both these methods posted on different sites.
Method 1
SELECT `blog_posts`.`title`, `blog_posts`.`pid`, `blog_posts`.`date` MATCH (`blog_posts`.`title`,`blog_posts`.`content`) AGAINST('+search* ~term' IN BOOLEAN MODE) AS relevance FROM `blog_posts` WHERE MATCH(`blog_posts`.`title`,`blog_posts`.`content`) AGAINST('+search* ~term' IN BOOLEAN MODE) >0 ORDER BY relevance DESC LIMIT 4;
Result -
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘MATCH (
blog_posts
.title
) AGAINST(‘review*’ IN BOOLEAN MODE) AS relevance FRO’ at line 1
Method 2
SELECT `blog_posts`.`title`, `blog_posts`.`pid`, `blog_posts`.`date` FROM `blog_posts` WHERE MATCH(`blog_posts`.`title`,`blog_posts`.`content`) AGAINST('+search* ~term' IN BOOLEAN MODE) AS relevance ORDER BY relevance DESC LIMIT 4;
Result -
#1054 - Unknown column ‘relevance’ in ‘order clause’
Any advice on which way should work and the fix to that problem would be much appreciated.
#2
My 2nd problem is when showing all the blog posts I’m trying to count the number of comments that blog post has on it. I’m already using 2 MySQL joins so I tried to add in a 3rd along with a COUNT(), this doesn’t seem to work -
SELECT
`blog_posts`.`title`,
`blog_posts`.`pid`,
`blog_posts`.`content`,
`blog_posts`.`date`,
`users`.`forename`,
`users`.`surname`,
`blog_categories`.`cat_name`,
COUNT(`blog_comments`.`co_id`) AS `comments`,
`blog_comments`.`delete`
FROM `blog_posts`
JOIN `users` ON `blog_posts`.`uid` = `users`.`uid`
JOIN `blog_categories` ON `blog_posts`.`cid` = `blog_categories`.`cid`
LEFT JOIN `blog_comments` ON `blog_posts`.`pid` = `blog_comments`.`pid`
WHERE `blog_posts`.`delete` = '0'
AND `blog_comments`.`delete` = '0'
ORDER BY `date`
DESC LIMIT 15;
Produces -
#1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),…) with no GROUP columns is illegal if there is no GROUP BY clause
It looks confusing but I’ve managed to get it working with 2 joins -
SELECT `blog_posts`.`title` , `blog_posts`.`pid` , `blog_posts`.`content` , `blog_posts`.`date` , `users`.`forename` , `users`.`surname` , `blog_categories`.`cat_name`
FROM `blog_posts`
JOIN `users` ON `blog_posts`.`uid` = `users`.`uid`
JOIN `blog_categories` ON `blog_posts`.`cid` = `blog_categories`.`cid`
WHERE `blog_posts`.`delete` = '0'
ORDER BY `date` DESC
LIMIT 15 ;
I appreciate I’ve written quite a lot, any help with either issue would be much appreciated, thanks!