Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 从mysql表中选择唯一列对上的行_Php_Mysql_Sql_Select_Distinct - Fatal编程技术网

Php 从mysql表中选择唯一列对上的行

Php 从mysql表中选择唯一列对上的行,php,mysql,sql,select,distinct,Php,Mysql,Sql,Select,Distinct,我有一张桌子桌子 现在它有三列表id,内容id,内容类型 我想要的是基于唯一的列对选择行 比如说,我有这样的行--> 我想要一个查询,选择id为->1,2,3,4,5,6的行。 不选择id为->7,8的行 因此,它得出结论:我不想选择行的完整重复项,你可以使用自连接来选择每个组的最小行 select t.* from test t join ( select min(id) id ,content_id,content_type from test grou

我有一张桌子
桌子

现在它有三列
表id
内容id
内容类型

我想要的是基于唯一的列对选择行

比如说,我有这样的行-->

我想要一个查询,选择id为->1,2,3,4,5,6的行。

不选择id为->7,8的行


因此,它得出结论:我不想选择行的完整重复项,你可以使用自连接来选择每个组的最小行

select t.* from 
test t
join (
      select min(id) id ,content_id,content_type
      from test
      group by content_id,content_type
   ) t1
on(t.id = t1.id 
   and t.content_id = t1.content_id 
   and t.content_type = t1.content_type)

或者,如果表中只有这3列,则只需使用
min()


这是特定于mysql的:如果使用GROUP BY函数而不使用聚合函数,GROUP BY子句将表现为distinct,并拾取第一个不同的行

select id, content_id, content_type from test group by content_id, content_type order by id

您可以使用@Khalid的解决方案,并将其更改为使用max()而不是min()。
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
select id, content_id, content_type from test group by content_id, content_type order by id