SQL搜索结果,按ID号

时间:2015-11-10 12:19:51

标签: php sql search group-by

我在尝试修复此SQL查询时遇到了一些问题

这是一个自定义搜索查询,在此CMS系统的所有网页上搜索'weddings'一词。

目前我在前5行显示相同的页面,因为'weddings'一词出现了5次。我想要做的是将具有相同ID号的行合并为1行,这样就不会出现多次。

我认为在这个语句的末尾做一个组会做到这一点,但我一直得到一个SQL语法错误

GROUP BY `documents`.`id`

我已经将完整的SQL语言附加了我目前得到的输出图像....任何想法?

SELECT `documents`.*, 
`documenttypes`.`name` as `doctype`, 
`articles`.`id` as `article_id`, 
`articles`.`language_id`, 
`articles`.`title`, 
`articles`.`template`, 
`articles`.`slug`, 
`articles`.`path`, 
`articles`.`slug_title`,
MATCH ( elements.textvalue )AGAINST ( 'weddings' ) AS score, 
elements.textvalue AS matching, 
LOWER(`articles`.`title`) 
LIKE '%weddings%' as 'like_title', 
( MATCH ( elements.textvalue ) 
AGAINST ( 'weddings' ) ) + IF(( LOWER(`articles`.`title`) 
LIKE '%weddings%'),1, 0) + IF((LOWER(`elements`.`textvalue`) 
LIKE '%weddings%'),1, 0) as total FROM (`documents`) 
LEFT JOIN `articles` ON `articles`.`document_id` = `documents`.`id` 
LEFT JOIN `documenttypes` ON `documents`.`documenttype_id` = `documenttypes`.`id` 
LEFT JOIN `documents_users` AS du ON `documents`.`id` = du.`document_id` 
LEFT JOIN `documents_usergroups` AS dug ON `documents`.`id` = dug.`document_id`
LEFT JOIN elements ON `elements`.`article_id` = `articles`.`id` 
WHERE `documents`.`trashed` = 0 
AND `documents`.`published` = 1 
AND `articles`.`status_id` = 1 
AND `articles`.`language_id` = 1 
AND (`documents`.`no_search` = '0' 
OR `documents`.`no_search` IS NULL) 
AND ( (dug.usergroup_id IS NULL) 
AND (du.user_id IS NULL) ) 
AND (`documents`.`startdate` < NOW() 
OR `documents`.`startdate` = '0000-00-00 00:00:00' OR `documents`.`startdate` IS NULL) 
AND (`documents`.`enddate` > NOW() 
OR `documents`.`enddate` = '0000-00-00 00:00:00' 
OR `documents`.`enddate` IS NULL) 
HAVING (total > 0) 
ORDER BY label ASC, 
total DESC LIMIT 0,10

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试使用语句DISTINCT

SELECT DISTINCT 'documents'.*, 
'documenttypes'.'name' as 'doctype', 
'articles'.'id' as 'article_id',
... 

GROUP BY可让您使用汇总功能,例如AVGMAXMINSUMCOUNT,显然您不会使用。