Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在联接查询中删除具有相同id的重复行?_Php_Mysql_Join_Kohana - Fatal编程技术网

Php 如何在联接查询中删除具有相同id的重复行?

Php 如何在联接查询中删除具有相同id的重复行?,php,mysql,join,kohana,Php,Mysql,Join,Kohana,我使用基于PHP的框架Kohana来创建一个搜索函数来搜索两个表中的所有名称,并且不会重复 两个表的结构如下所示: content - content_revision 我尝试使用我的代码,如: $contents = Jelly::select("content") ->join('content_revision') ->on('content.id', '=', 'content

我使用基于PHP的框架Kohana来创建一个搜索函数来搜索两个表中的所有名称,并且不会重复

两个表的结构如下所示:

content       -       content_revision

我尝试使用我的代码,如:

$contents = Jelly::select("content")
                  ->join('content_revision')
                  ->on('content.id', '=', 'content_revision.content_id')
                  ->or_where("content.title", "LIKE", "%" . $value . "%")
                  ->or_where("content_revision.title", "LIKE", "%" . $value . "%");
它显示的结果不够,结果中所有行的id都相同

因为表
content\u revision
中的我的数据有许多重复的行(重复后content\u id相同)

我尝试使用
左连接
,但它也显示了相同的结果

我认为使用
distinct
关键字只获取行,这是唯一的

所以,我在互联网上研究,但没有任何文件定制

我找到了这个链接:

但是找不到服务器

有什么办法解决我的问题吗


谢谢。

你应该改为试试分组

$contents = Jelly::select("content")
              ->join('content_revision')
              ->on('content.id', '=', 'content_revision.content_id')
              ->or_where("content.title", "LIKE", "%" . $value . "%")
              ->or_where("content_revision.title", "LIKE", "%" . $value . "%")
              ->group_by("content.id");

试试这个,因为它只会给你最后一个版本来比较标题

$contents = Jelly::select("content")
              ->join('(SELECT CR.content_id,CR.title, MAX(CR.id) AS maxid
            FROM content_revision CR GROUP BY CR.content_id) as LatestRevision')
              ->on('content.id', '=', 'LatestRevision.content_id')
              ->or_where("content.title", "LIKE", "%" . $value . "%")
              ->or_where("LatestRevision.title", "LIKE", "%" . $value . "%")
              ->group_by("content.id");

和我一起工作。但我还想获取表
内容的数据。所以,
join
似乎无法从两个表中获取所有数据。我在这里读到:。因此,我尝试使用
完全外部联接
,但似乎不起作用。我在MySQL数据库工作。谢谢
$contents = Jelly::select("content")
              ->join('(SELECT CR.content_id,CR.title, MAX(CR.id) AS maxid
            FROM content_revision CR GROUP BY CR.content_id) as LatestRevision')
              ->on('content.id', '=', 'LatestRevision.content_id')
              ->or_where("content.title", "LIKE", "%" . $value . "%")
              ->or_where("LatestRevision.title", "LIKE", "%" . $value . "%")
              ->group_by("content.id");