简体   繁体   English

SQL搜索结果,按ID号分组

[英]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使您可以使用聚合函数,例如AVGMAXMINSUMCOUNT ,这些函数显然不使用。

将结果对话 ID 分组为一个<div>标签</div><div id="text_translate"><p>我有一个聊天表,我想为每个对话 ID 或conv_id行将消息放入一个 div 标签中。<br><br> <strong>这是我的聊天表:</strong><br> <a href="https://i.stack.imgur.com/UiU1H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UiU1H.png" alt="在此处输入图像描述"></a></p><hr><p> <strong>这是我的 php 代码:</strong><br></p><pre> &lt;?php require 'config/config.php'; mysqli_set_charset($conn,"utf8"); $from = 6; $to = 3; $stmt_msg = $conn-&gt;prepare(" SELECT * FROM ( SELECT id, message, from_id, to_id, conv_id FROM chat WHERE (from_id =? AND to_id =?) OR (to_id =? AND from_id =?) ORDER BY send_date desc LIMIT 20 ) as tmp ORDER BY tmp.id "); $stmt_msg-&gt;bind_param("iiii", $to, $from, $to, $from); if ($stmt_msg-&gt;execute()) { $query_gmsgs = $stmt_msg-&gt;get_result(); if (mysqli_num_rows($query_gmsgs) &gt; 0) { while ($rowmsg = $query_gmsgs-&gt;FETCH_ARRAY()) { $message = htmlspecialchars($rowmsg['message']); $to_id = htmlspecialchars($rowmsg['to_id']); $from_id = htmlspecialchars($rowmsg['from_id']); $send_date = htmlspecialchars($row['send_date']); $send_datev = date_create($send_date); $send_datev = date_format($send_datev, 'd/m/y'); $conv_id = htmlspecialchars($rowmsg['conv_id']); if($from_id == $from){ echo "&lt;div class='from $conv_id'&gt;&lt;p&gt;Me: $message&lt;/p&gt;&lt;/div&gt;"; }else{ echo "&lt;div class='to $conv_id'&gt;&lt;p&gt;Other: $message&lt;/p&gt;&lt;/div&gt;"; } } } }?&gt;</pre><p> <strong>这是 web 浏览器中显示的结果:</strong> <a href="https://i.stack.imgur.com/OUbxQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OUbxQ.png" alt="在此处输入图像描述"></a></p><p> 如您所见,我们有多个&lt;div&gt;标签用于conv_id ,每个 Message 都有一个&lt;div&gt;标签。 我想将每个对话分组到一个 div。<br> <strong>例如:</strong><br> 消息 1 和消息 2 有用户 6 发送的两条消息,它们的 conv_id 为 6-1。 我想将这些消息 [1, 2] 分组到一个 div。 等等。 谢谢。 顺便说一句<strong>,我想要这个</strong>:<br><br> <a href="https://i.stack.imgur.com/8bo8s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8bo8s.png" alt="在此处输入图像描述"></a></p></div> - Group result conversation id into one <div> tag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取搜索结果的ID - Getting search result's id 搜索结果返回带有搜索查询的“随机数” - Search result return “random number” with search query 如何在SQL中获取结果组 - How to get result group by in SQL php-mysqli-在sql查询中是否搜索ID,如何在此结果上方也获取5行并在其下方获取5行 - php-mysqli - In sql query if search for ID, how to get also 5 row above and 5 rows below this result id 降序的 SQL 分组依据 - SQL group by with descending id order 如何分离搜索栏中的结果并将其组合在一起? - How to separate result in search bar and group it together? 将结果对话 ID 分组为一个<div>标签</div><div id="text_translate"><p>我有一个聊天表,我想为每个对话 ID 或conv_id行将消息放入一个 div 标签中。<br><br> <strong>这是我的聊天表:</strong><br> <a href="https://i.stack.imgur.com/UiU1H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UiU1H.png" alt="在此处输入图像描述"></a></p><hr><p> <strong>这是我的 php 代码:</strong><br></p><pre> &lt;?php require 'config/config.php'; mysqli_set_charset($conn,"utf8"); $from = 6; $to = 3; $stmt_msg = $conn-&gt;prepare(" SELECT * FROM ( SELECT id, message, from_id, to_id, conv_id FROM chat WHERE (from_id =? AND to_id =?) OR (to_id =? AND from_id =?) ORDER BY send_date desc LIMIT 20 ) as tmp ORDER BY tmp.id "); $stmt_msg-&gt;bind_param("iiii", $to, $from, $to, $from); if ($stmt_msg-&gt;execute()) { $query_gmsgs = $stmt_msg-&gt;get_result(); if (mysqli_num_rows($query_gmsgs) &gt; 0) { while ($rowmsg = $query_gmsgs-&gt;FETCH_ARRAY()) { $message = htmlspecialchars($rowmsg['message']); $to_id = htmlspecialchars($rowmsg['to_id']); $from_id = htmlspecialchars($rowmsg['from_id']); $send_date = htmlspecialchars($row['send_date']); $send_datev = date_create($send_date); $send_datev = date_format($send_datev, 'd/m/y'); $conv_id = htmlspecialchars($rowmsg['conv_id']); if($from_id == $from){ echo "&lt;div class='from $conv_id'&gt;&lt;p&gt;Me: $message&lt;/p&gt;&lt;/div&gt;"; }else{ echo "&lt;div class='to $conv_id'&gt;&lt;p&gt;Other: $message&lt;/p&gt;&lt;/div&gt;"; } } } }?&gt;</pre><p> <strong>这是 web 浏览器中显示的结果:</strong> <a href="https://i.stack.imgur.com/OUbxQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OUbxQ.png" alt="在此处输入图像描述"></a></p><p> 如您所见,我们有多个&lt;div&gt;标签用于conv_id ,每个 Message 都有一个&lt;div&gt;标签。 我想将每个对话分组到一个 div。<br> <strong>例如:</strong><br> 消息 1 和消息 2 有用户 6 发送的两条消息,它们的 conv_id 为 6-1。 我想将这些消息 [1, 2] 分组到一个 div。 等等。 谢谢。 顺便说一句<strong>,我想要这个</strong>:<br><br> <a href="https://i.stack.imgur.com/8bo8s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8bo8s.png" alt="在此处输入图像描述"></a></p></div> - Group result conversation id into one <div> tag 如何抓取搜索结果的id来自 - How to grab the id of the search result came from 使用id号搜索字符串并用img标签替换id号 - Search string and replace id number with img tag using id number 搜索结果后如何更新库存编号? - How to update instock number after search result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM