[英]SQL search Result, Group by ID number
I'm having some issues with trying to fix this SQL Query 我在尝试修复此SQL查询时遇到一些问题
This is a custom search query which is searching for the word 'weddings' on all pages on this CMS system. 这是一个自定义搜索查询,正在此CMS系统的所有页面上搜索单词“ weddings” 。
At the moment I am getting the same page appear on the first 5 rows because the word 'weddings' appears 5 times. 目前,我的前5行显示的是同一页面,因为“婚礼”一词出现了5次。 What I want to do is combine the rows with the same ID number into 1 row so it doesn't appear multiple times. 我想做的是将具有相同ID号的行合并为1行,这样它就不会出现多次。
I thought doing a group by at the end of this statement would do this but I keep getting an SQL syntax error 我以为在此语句末尾进行分组会做到这一点,但是我一直收到SQL语法错误
GROUP BY `documents`.`id`
I have attached the full SQL bellow with an image of the output i currently get.... Any idea? 我已经将完整的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
You can try to use the statement DISTINCT
: 您可以尝试使用语句DISTINCT
:
SELECT DISTINCT 'documents'.*,
'documenttypes'.'name' as 'doctype',
'articles'.'id' as 'article_id',
...
GROUP BY
lets you use aggregate functions, like AVG
, MAX
, MIN
, SUM
, and COUNT
which apparently you don't use. GROUP BY
使您可以使用聚合函数,例如AVG
, MAX
, MIN
, SUM
和COUNT
,这些函数显然不使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.