如果 id 和name 一一对应 (id不是主键)select distinct * from yy如果 id 和name 不是一一对应 (id是主键)select max(id),name,tel from tb group by name,tel --取最大的id
select distinct * from yy
在yy表中,id 如果作为主键的话,这个字段的值就不能出现重复。你提供的数据就有错误。 就你现在的表中的数据要想得到那样的查询结果的话,你可以这样完成:--测试语句: create table yy(id varchar(4) ,name varchar(8),tel varchar(16)) --插入数据 insert yy select '001','jim','010-2654815' union all select '003','smish','029-6235844' union all select '001','jim','010-2654815' union all select '001','jim','010-2654815' union all select '003','smish','029-6235844' --select * from yy ---你可以这样查询: select distinct * from yy ---也可以这样查询: select * from yy group by id ,name,tel
select distinct name from yy select distinct id from yy 不过主键设置确实有错,主键不能有重复的。
select id,name,tel from tb group by id,name,tel 与select distinct * from yy 有什么不同?
id 不应是主键!
id 不应是主键!
就你现在的表中的数据要想得到那样的查询结果的话,你可以这样完成:--测试语句:
create table yy(id varchar(4) ,name varchar(8),tel varchar(16))
--插入数据
insert yy select '001','jim','010-2654815'
union all select '003','smish','029-6235844'
union all select '001','jim','010-2654815'
union all select '001','jim','010-2654815'
union all select '003','smish','029-6235844'
--select * from yy ---你可以这样查询:
select distinct * from yy
---也可以这样查询:
select * from yy group by id ,name,tel
select distinct id from yy
不过主键设置确实有错,主键不能有重复的。
与select distinct * from yy
有什么不同?