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